2

I want to make a new array by adding an element to an existing array by "push()" method.

This is the existing array:

let arr = [{label: 1, value: 1}, {label: 2, value: 2}];

This is the element I want to add to the existing array:

{label: 3, value: 3}

So this is the full code with "push()" method:

let arr = [{label: 1, value: 1}, {label: 2, value: 2}];

let newArr = arr.push({label: 3, value: 3});

console.log(newArr); // 3

But push() method returns the length of the new array which is "3" to "newArr" variable. However, what I really want is the actual new array instead of its length for "newArr" variable.

Are there any ways to get the actual new array for "newArr" variable?

VLAZ
  • 26,331
  • 9
  • 49
  • 67
RamAlx
  • 6,976
  • 23
  • 58
  • 106
  • The actual array is still `arr`. `push` mutates the original array that you call it on. – Joe Clay May 02 '17 at 12:21
  • you need to look to `arr`. its is just pushed to it. push returns the length after the insert – Nina Scholz May 02 '17 at 12:21
  • [array.push](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/push?v=example) as per documentation, returns the length after the item has been added – Esko May 02 '17 at 12:22

7 Answers7

8

With push you are actually mutating the original array. Immutable array extending is only available in ES2015+ (supported by all current, major browsers). You can use the spread operator ...:

const original = [1, 2];
const next = [...original, 3];
console.log(next); // [1, 2, 3]

Also new is a reserved keyword and can't be used as an identifier.

4

First of all, the new keyword has a specified role in javascript, you can't use it as a variable name.

Reserved keywords in javascript.

Secondly, push method works in situ, you don't have to assign it to a new variable. It won't return a new array, but modify the original one.

var arr = [{label: 1, value: 1}, {label:2, value:2}];
    arr.push({label:3, value:3});
    
    console.log(arr);
kind user
  • 40,029
  • 7
  • 67
  • 77
2

user7334203, you wrote that arr.push({label:3, value:3}) mean that your new object push in your array so simply print your array so you got your array.

ex

var a = [{label: 1, value: 1}, {label:2, value:2}],
a.push({label:3, value:3}),
var new = a; 
console.log(new)
1

Don't use const .simply do with arr#push .its enough to add with array

var arr= [{label: 1, value: 1}, {label:2, value:2}] 
arr.push({label:3, value:3}) 
 console.log(arr)
prasanth
  • 22,145
  • 4
  • 29
  • 53
1

A good reference for leaning JS is MDN. If you look at the Array#push spec, you'll see that it mutates the input array, returning the new length of the array.

sp00m
  • 47,968
  • 31
  • 142
  • 252
1

From the MDN documentation push returns the new length of your array, neither the new array, nor the added value.

The push function alters the array in place, meaning it will change the original array (you don't actually need to re-assign it)!

You could have the result you were expecting though by altering your code to: arr.push({label:3, value:3});

then assign to a new array like: const newArr = arr;

ioakeimo
  • 99
  • 7
  • Be aware though, `newArr` and `arr` target the same array object, so modifying `newArr` (e.g. by `push`ing) will modify `arr` as well. See @OleBläsing answer for a non-mutating solution. – sp00m May 02 '17 at 12:37
  • 1
    That's true. A solution to that would probably be using `const newArr = arr.slice();` but I tried avoiding getting into more detail and for now answering as simply as I could. It was indeed my mistake though and thanks for pointing that out ;) – ioakeimo May 02 '17 at 12:44
0

You can use "concat()" method to get the actual new array for "newArr" variable like below:

let arr = [{label: 1, value: 1}, {label: 2, value: 2}];

let newArr = arr.concat({label: 3, value: 3});

console.log(newArr); 
// [
//   {label: 1, value: 1}, 
//   {label: 2, value: 2}, 
//   {label: 3, value: 3}
// ]

This code doesn't need "newArr" variable. The actual new array is assigned to the original variable "arr":

let arr = [{label: 1, value: 1}, {label: 2, value: 2}];

arr = arr.concat({label: 3, value: 3});

console.log(arr);
// [
//   {label: 1, value: 1},
//   {label: 2, value: 2},
//   {label: 3, value: 3}
// ]
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129