So, For instance, I have the following array
var sections = [{ name: 'apple', count: 20 },
{ name: 'banana', count: 80 },
{ name: 'oranges', count: 10 }]
Now, if I want to add more items, then I can do
sections.push({ name: 'mango', count: '32' });
which will add the new array to sections.
Now, suppose, I have another array that shows where the fruit came from and how many.
var country = [{ country: 'usa', count: 5 },
{ country: 'brazil', count: 27 }]
So, I want to add this new country
array to the third element of sections
with a new prop called country as the following.
var sections = [{ name: 'apple', count: 20 },
{ name: 'banana', count: 80 },
{ name: 'oranges', count: 10,
country: [{ country: 'usa', count: 5 },
{ country: 'brazil', count: 27 }]
}]
How can I do this? I tried push
, splice
but couldn't figure it out at all.
I also tried sections[3].push.apply(country).. and many other things that I can't recall anymore. Either I received an error or just didn't push the array as I wanted to.