简体   繁体   English

在Javascript对象数组中,如果该元素本身是一个对象数组,如何通过名称/ ID来定位特定的数组元素?

[英]In a Javascript array of objects, how can I target a specific array element by name/ID if that element is an array of objects itself?

If I push objects created on the fly, including another internal array of objects, to an array, how can I add objects only to a specific object's array. 如果我将动态创建的对象(包括另一个内部对象数组)推送到数组,如何只将对象添加到特定对象的数组中。 Using Reviews for example: 使用评论例如:

reviews.push({ 'reviewer' : 'John', 
               'review' : 'This movie was the greatest', 
               'comments' : { 'commenter' : 'Dave',
                              'comment' : 'The review by John was too short'} 
             });

'comments' being the internal array of objects. 'comments'是对象的内部数组。

If at a later stage I want to add objects only to a specific object's 'comments' by it's parents name/ID, is it possible? 如果在稍后阶段我想通过它的父母姓名/ ID仅将对象添加到特定对象的“评论”中,是否可能? And if so how would I go about doing it? 如果是这样,我将如何去做呢?

Using the example, If there is only 1 review with multiple comments, and the comments come at a later stage, how can I add new comments to the 'comments' array within the review object? 使用该示例,如果只有1条评论带有多条评论,并且评论会在稍后阶段发布,那么如何在评论对象中的“评论”数组中添加新评论?

I am guessing I would need to have a review ID to stop duplicates etc, but that aside I'm not even sure syntactically how to do it or if it's even possible. 我猜我需要有一个评论ID来阻止重复等,但除此之外,我甚至不确定语法是如何做到的,或者甚至可能。

Thanks in advance. 提前致谢。

First off, comments should be an array and you have an object. 首先,注释应该是一个数组,你有一个对象。

Then proceed like this: 然后继续这样:

reviews[someIndex].comments.push( { commenter: 'john', comment: 'hello!!'});

Ie

if you need to add a comment to the first post do: 如果你需要在第一篇文章中添加评论,请执行以下操作:

reviews[0].comments.push( { /* your comment */ } );

EDIT : I always used get() to retrieve an index but I seem to not be able to find much documentation about it, so I'm assuming (though I'm not sure) that [i] is a more standard way of doing things, hence why I changed the example. 编辑 :我总是使用get()来检索索引,但我似乎无法找到很多关于它的文档,所以我假设(虽然我不确定)[i]是一种更标准的做法事情,因此我改变了这个例子。 However the concept of pushing into comments doesn't change. 然而,推进评论的概念并没有改变。

EDIT 2 : OP wants to target a particular element in an array. 编辑2 :OP想要定位数组中的特定元素。 I would use Array.prototype.indexOf as described here where you can find a solution for browsers that do not support indexOf as well. 我将使用此处所述的Array.prototype.indexOf,您可以其中找到不支持indexOf的浏览器的解决方案。

If you want to keep track of the reviews and comments by id's I'd do something like: 如果你想跟踪id的评论和评论,我会做类似的事情:

var reviews = {};
review_id = "#1234";
reviews[review_id] = {
    "reviewer": "John",
    "review": "This movie was the bestest",
    "comments": {
        "#456": {
            "commenter": "HAL 9000",
            "comment": "I'm afraid I can't let you do that Dave"
        }
    }
};

Adding a comment: 添加评论:

reviews[review_id].comments[comment_id] = { /* .. */ };
for(var i=0;i<reviews.length;i++){
if(reviews[i]["reviewer"]=="John"){
   var count=Object.keys(reviews[i].comments).length/2;
   reviews[i].comments["commenter"+(count+1)]="another commenter";
   reviews[i].comments["comment"+(count+1)]="some other comment";
   console.log(count);
}
}

output 产量

commenter "Dave" 评论者“戴夫”

comment "The review by John was too short" 评论“约翰的评论太短了”

commenter2 "another commenter" commenter2“另一位评论者”

comment2 "some other comment" 评论2“其他一些评论”

http://jsfiddle.net/gkxcj/ http://jsfiddle.net/gkxcj/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM