Reputation: 29458
I don't understand why the splice method isn't working for me. I have an array that looks like: (it is actually bigger but I didn't want to clutter the page)
var navItems = [ {
"content": "Panels",
"icon": "panels"
},
{
"content": "Samples",
"icon": "sample"
}];
I want to insert an item say in the middle:
var testNavItems = navItems.splice(1, 0, {
"content": "New Nav",
"icon": "New Nav"
});
console.log(testNavItems);
OR
var testNavItems = navItems.push({
"content": "New Nav",
"icon": "New Nav"
});
console.log(testNavItems);
I get an empty array. Is this possible in JS?
Upvotes: 0
Views: 51
Reputation: 190943
.push
and .splice
do not create a new array. They modify the original array.
Try
console.log(navItems);
Upvotes: 3