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',
'ments' : { 'menter' : 'Dave',
'ment' : 'The review by John was too short'}
});
'ments' being the internal array of objects.
If at a later stage I want to add objects only to a specific object's 'ments' by it's parents name/ID, is it possible? And if so how would I go about doing it?
Using the example, If there is only 1 review with multiple ments, and the ments e at a later stage, how can I add new ments to the 'ments' array within the review object?
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.
Thanks in advance.
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',
'ments' : { 'menter' : 'Dave',
'ment' : 'The review by John was too short'}
});
'ments' being the internal array of objects.
If at a later stage I want to add objects only to a specific object's 'ments' by it's parents name/ID, is it possible? And if so how would I go about doing it?
Using the example, If there is only 1 review with multiple ments, and the ments e at a later stage, how can I add new ments to the 'ments' array within the review object?
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.
Thanks in advance.
Share Improve this question edited Aug 16, 2013 at 9:44 Aaron asked Aug 16, 2013 at 9:16 AaronAaron 3,2657 gold badges32 silver badges49 bronze badges 1-
1
ments is an object at the moment, should it be wrapped in
[ .. ]
? – Russ Cam Commented Aug 16, 2013 at 9:18
3 Answers
Reset to default 4First off, ments should be an array and you have an object.
Then proceed like this:
reviews[someIndex].ments.push( { menter: 'john', ment: 'hello!!'});
I.e.
if you need to add a ment to the first post do:
reviews[0].ments.push( { /* your ment */ } );
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. However the concept of pushing into ments doesn't change.
EDIT 2: OP wants to target a particular element in an array. I would use Array.prototype.indexOf as described here where you can find a solution for browsers that do not support indexOf as well.
If you want to keep track of the reviews and ments by id's I'd do something like:
var reviews = {};
review_id = "#1234";
reviews[review_id] = {
"reviewer": "John",
"review": "This movie was the bestest",
"ments": {
"#456": {
"menter": "HAL 9000",
"ment": "I'm afraid I can't let you do that Dave"
}
}
};
Adding a ment:
reviews[review_id].ments[ment_id] = { /* .. */ };
for(var i=0;i<reviews.length;i++){
if(reviews[i]["reviewer"]=="John"){
var count=Object.keys(reviews[i].ments).length/2;
reviews[i].ments["menter"+(count+1)]="another menter";
reviews[i].ments["ment"+(count+1)]="some other ment";
console.log(count);
}
}
output
menter "Dave"
ment "The review by John was too short"
menter2 "another menter"
ment2 "some other ment"
http://jsfiddle/gkxcj/