0

I thought the below is logically sound in adding or inserting object into an object array but the result I am getting is funky. Can someone please tell me what I am doing wrong here?? Why is below code returning empty array when I am trying to insert an object into an object array in the index of 0 and why is below code returning 4 when I am trying to add an object at the end of the object array?

let objArr = [{
    id: 1,
    company: "Rapid Precision Mfg.",
    title: "Quality Engineer",
    firstName: "Dongyob",
    lastName: "Lee",
    officePh: "",
    ext: "",
    cell: "669-294-0910",
    email: "[email protected]"
  },
  {
    id: 2,
    company: "Facebook",
    title: "Frontend Developer",
    firstName: "Edward",
    lastName: "Simmons",
    officePh: "408-516-4662",
    ext: "003",
    cell: "669-252-4251",
    email: "[email protected]"
  }
]
let nobj = {
  id: 1,
  company: "Rapid Precision Mfg.",
  title: "Quality Engineer",
  firstName: "Dongyob",
  lastName: "Lee",
  officePh: "",
  ext: "",
  cell: "669-294-0910",
  email: "[email protected]"
}
console.log(objArr.splice(0, 0, nobj)) //Outcome: []
console.log(objArr.push(nobj)) //Outcome: 4

3 Answers 3

3

splice returns the removed elements of the array. If you don't remove any elements in the splice call, the returned array will be empty.

const arr = [0, 1, 2, 3];
// from index 0, don't remove any elements, and insert element 'foo':
console.log(arr.splice(0, 0, 'foo'));

push returns the new length of the array. The output is 4 because the array started with 2 items, you spliced one item in (making the length 3), then pushed another item in, making the length 4.

Your current code

console.log(objArr.splice(0, 0, nobj))
console.log(objArr.push(nobj))

is inserting nobj into the last position and into the first position in the array - if you want to see what the array is afterwards, log the array:

console.log(objArr);

Note that rather than spliceing in an element at index 0, you can use unshift instead:

const arr = [0, 1, 2, 3];
arr.unshift('foo');
console.log(arr);

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much all I needed to do is console.log(objArr), I am an idiot!
0

The splice syntax is array.splice(index, howmany, item1, ....., itemX) and it returns the removed one.

If you want to test your change do console.log(objArr)

let objArr = [{
    id: 1,
    company: "Rapid Precision Mfg.",
    title: "Quality Engineer",
    firstName: "Dongyob",
    lastName: "Lee",
    officePh: "",
    ext: "",
    cell: "669-294-0910",
    email: "[email protected]"
  },
  {
    id: 2,
    company: "Facebook",
    title: "Frontend Developer",
    firstName: "Edward",
    lastName: "Simmons",
    officePh: "408-516-4662",
    ext: "003",
    cell: "669-252-4251",
    email: "[email protected]"
  }
]
let nobj = {
  id: 1,
  company: "Rapid Precision Mfg.",
  title: "Quality Engineer",
  firstName: "Dongyob",
  lastName: "Lee",
  officePh: "",
  ext: "",
  cell: "669-294-0910",
  email: "[email protected]"
}
objArr.splice(0, 0, nobj)
console.log(objArr) //Outcome: []
//console.log(objArr.push(nobj))

2 Comments

Thank you so much! You answer helped but unfortunately, I chose the first person who responded. Thanks!
@LeeDongyobEric thats ok no problem
0

As the above users have mentioned, splice() returns the removed element(s) of the array. If you are trying to push an object to the start of your array, another way to do so would be to use the spread syntax (yay ES6!!). I think it is much more readable than the method you are trying to employ.

//[nobj, ...objArr]
console.log([nobj, ...objArr]);

Unlike unshift(), it does not modify the original objArr array in-place.

const newArray = [nobj, ...objArr];

2 Comments

This is very useful, an immutable way to add an object to array.
Yup! Good concept to remember, especially if you are dealing a lot with React/Redux!

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.