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 push objects created on fly, including internal array of objects, array, how can add objects specific object's array. using reviews example:
reviews.push({ 'reviewer' : 'john', 'review' : 'this movie greatest', 'comments' : { 'commenter' : 'dave', 'comment' : 'the review john short'} });
'comments' being internal array of objects.
if @ later stage want add objects specific object's 'comments' it's parents name/id, possible? , if how go doing it?
using example, if there 1 review multiple comments, , comments come @ later stage, how can add new comments 'comments' array within review object?
i guessing need have review id stop duplicates etc, aside i'm not sure syntactically how or if it's possible.
thanks in advance.
first off, comments should array , have object.
then proceed this:
reviews[someindex].comments.push( { commenter: 'john', comment: 'hello!!'});
i.e.
if need add comment first post do:
reviews[0].comments.push( { /* comment */ } );
edit : used get() retrieve index seem not able find documentation it, i'm assuming (though i'm not sure) [i] more standard way of doing things, hence why changed example. concept of pushing comments doesn't change.
edit 2: op wants target particular element in array. use array.prototype.indexof described here can find solution browsers not support indexof well.
Comments
Post a Comment