0

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?

1
  • 1
    .splice returns the elements which have been removed, .push returns the new length of the array. Always check the documentation. Commented Mar 26, 2013 at 18:43

1 Answer 1

3

.push and .splice do not create a new array. They modify the original array.

Try

console.log(navItems);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.