0

I have an Array List.

dataList = []

I want to insert object in array, So I tried this,

dataList.concat([{"name":"BOB", "value":"1"}])
// [{"name":"BOB", "value":"1"}]  

but when I insert 2nd object in the same array.

dataList.concat([{"name":"Joe", "value":"2"}])
// [{"name":"Joe", "value":"2"},{"name":"Joe", "value":"2"}] 

after inserting second array it replaces first object also. where am I wrong?, Please help.

Amir
  • 11
  • 5
  • To insert a new value in array, use `Array.push`. `Array.concat` returns a new array – Rajesh Jan 03 '18 at 05:37
  • I have tried with push method also . It does the same in my case. – Amir Jan 03 '18 at 05:38
  • 1
    Do you have a variable that holds object or you have inline object? – Rajesh Jan 03 '18 at 05:39
  • i have inline array of object. dataList = [{"name":"BOB", "value":"1"}] newDataList = [{"name":"Joe", "value":"2"}] dataList.concat(newDataList) // [{"name":"Joe", "value":"2"},{"name":"Joe", "value":"2"}] – Amir Jan 03 '18 at 05:40
  • 1
    Looking at the behavior, you must have it in a variable. The issue is, objects are assigned using reference. So changing 1 variable will update value in all variables holding this reference – Rajesh Jan 03 '18 at 05:41
  • Your example doesn't do what you say. `concat` doesn't merge arrays in place, it returns a new array. – Teemu Jan 03 '18 at 05:42
  • what output you are looking for? – Negi Rox Jan 03 '18 at 05:49

2 Answers2

0

Simply use push function

var dataList = [];
dataList.push({
  "name": "BOB",
  "value": "1"
});
//Pushing second object
dataList.push({
  "name": "Joe",
  "value": "2"
})
console.log(dataList)
brk
  • 48,835
  • 10
  • 56
  • 78
0

To push a value in an array, you will have to use array.push. array.concat does not adds new item. it merges 2 array and return a third array. So your code:

dataList.concat([{"name":"BOB", "value":"1"}])

does not do anything.

var dataList = []
dataList.concat([{"name":"BOB", "value":"1"}])

console.log(dataList)

As per the behavior, your code should look something like this:

var dataList = []
var obj = [{"name":"BOB", "value":"1"}];
dataList = dataList.concat(obj)
obj[0].value = 2;
dataList = dataList.concat(obj)
console.log(dataList)

The reason both the objects are being affected is because, objects are copied/assigned using reference. So a variable will hold a memory location and any changes to it will be reflected to this location. So if you have more than 1 variable holding this reference, it will also get updated.


So how should you achieve this?

var dataList = []
var obj = {"name":"BOB", "value":"1"};
dataList = dataList.concat(obj)
var obj2 = Object.assign({}, obj)
obj2.value = 2;
dataList = dataList.concat(obj2)
console.log(dataList)

But still using Array.concat is wrong. It is not intended to be used like this.

var dataList = []
var obj = {"name":"BOB", "value":"1"};
dataList.push(obj)
var obj2 = Object.assign({}, obj)
obj2.value = 2;
dataList.push(obj2)
console.log(dataList)

References:

Community
  • 1
  • 1
Rajesh
  • 24,354
  • 5
  • 48
  • 79
  • **Note:** Not closing as dupe as OP has confirmed he/she has tried using `.push` but it did not solve the issue. So thought of explaining what and why. – Rajesh Jan 03 '18 at 06:05