0

I am trying to add json object to json array, but seems something wrong. what I'm getting array elements as -

0:{}
    local:"con"
    name:"con"
    order:15
1:{}
    local:"con"
    name:"con"
    order:15

what I'm expecting to happen -

0:{}
    local:"con"
    name:"con"
    order:15
1:{}
    local:"con2"
    name:"con2"
    order:16

I want points array should have all unique json objects(with very for loop iteration) but it replaces all previously added json with current json(of current iteration) object and entire array has same json objects at all index positions
code -

for(var i = 0; i < machineDetails.length; i++)
{

          machine['name'] = machineDetails[i].name;
          machine['local'] = machineDetails[i].localName;
          machine['order'] = machineDetails[i].orderInLine;
          points.push(machine);
          console.log(points);
}

in addition to above I tried points[i].push(machine); but it does not work either and throws error as push property is not defined. Please point me in right direction what i am missing or what should I do?

Dev
  • 2,739
  • 2
  • 21
  • 34
  • 1
    you are not creating a new machine object. All changes are happening on same machine object – Abhiroj Panwar Jul 09 '19 at 08:46
  • Possible duplicate of [How do I correctly clone a JavaScript object?](https://stackoverflow.com/questions/728360/how-do-i-correctly-clone-a-javascript-object) – MauriceNino Jul 09 '19 at 08:47
  • @AbhirojPanwar thanks !! exactly what I was seeking, can you tell what is workaround for same should I do it inside for loop `machine[i]` and then add objects to ith object of same? – Dev Jul 09 '19 at 08:58
  • It is exactly the same. Maybe read other StackOverflow Questions that answer the same thing more closely. Your problem is that you only change an object and push a reference. You would need to *clone* the object or create a new one for that to change. (depends on the use-case) – MauriceNino Jul 09 '19 at 09:06
  • they might be but I wont get what I was missing or what I was doing? that's the main intent to ask new question more over I looked other question already those are not answering what I was missing – Dev Jul 09 '19 at 09:11

2 Answers2

1

You need to move an assignment of an empty object inside of the loop, to prevent the same object reference, which leads to same values inside.

for (var i = 0; i < machineDetails.length; i++) {
    machine = {};

or even shorter:

for (var i = 0; i < machineDetails.length; i++) {
    points.push({
        name: machineDetails[i].name,
        local: machineDetails[i].localName,
        order: machineDetails[i].orderInLine
    });
}
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

When using arrays you can use different types of iterations including array.map which seems to be exactly what you are doing: Mapping the array called machineDetails to a new one called points:

const points = machineDetails.map(({ name, localName, orderInLine}) => ({
  name,
  local: localName,
  order: orderInLine,
});

Using map since you are creating a new scope and returning a new object every time the issue in hand which is overriding attributes of an object which you have already referenced it into your array is not going to happen.

Mehran Hatami
  • 12,723
  • 6
  • 28
  • 35