0

I have Array:

[
  { 
    name: 'Bob',
    nestedArray: [ { param: '' }, { param: ''}, ... ]
  },
  { 
    name: 'Alise',
    nestedArray: [ { param: '' }, { param: ''}, ... ]
  },
  { 
    name: 'Rony',
    nestedArray: [ { param: '' }, { param: ''}, ... ]
  },

  ....

]

Need to make Array.nestedArray.param = Array.name

I do:

Array.forEach((arrayElement) => {
  arrayElement.nestedArray.forEach((nestedArrayElement) => {
    console.log(arrayElement.name);
    nestedArrayElement.param = arrayElement.name;
  });
});

console.log(Array);

Checking resulting Array I get param equal to 'Rony' (last iterated arrayElement) in all nestedArrays. But the string

console.log(arrayElement.name);

always returns expected value ('Bob', 'Alise', 'Rony')

I tried index access also to read and write.

Help me please. What am I doing wrong?

napilnik
  • 117
  • 2
  • 8
  • You must be doing something else. When I run your exact code I get the expected outcome – tymeJV Jul 01 '16 at 20:34
  • Is your array really called `Array`? You may want to call it something else as `Array` is already used by native Javascript. – jaybee Jul 01 '16 at 20:42
  • Can't reproduce: https://jsfiddle.net/barmar/sn8h5Lzk/1/ – Barmar Jul 01 '16 at 20:48
  • No it's called not Array it's just to be clear in my explanations. – napilnik Jul 01 '16 at 20:48
  • I think the problem is with the way you constructed `Array`. I'll bet all the objects in `nestedArray` are the same object, not new objects. – Barmar Jul 01 '16 at 20:49
  • It works fine if you use literals as you show in the question. – Barmar Jul 01 '16 at 20:49
  • The problem was in how I managed to create "nestedArray" – it was an array containing links to same object. So each time it was manipulations with same object here: nestedArrayElement.param = arrayElement.name; Thanks to everybody trying to help! – napilnik Jul 03 '16 at 19:53

1 Answers1

0

If I'm understanding your question correctly:

var arr = [
  { name: 'Bob', nestedArray: [{ param: '' }, { param: ''}] },
  { name: 'Alice', nestedArray: [{ param: '' }, { param: ''}] },
  { name: 'Rony', nestedArray: [{ param: '' }, { param: ''}] },
];

for (var i in arr) {
    for (var j in arr[i].nestedArray) {
    arr[i].nestedArray[j].param = arr[i].name;
  }
}

Edit: Fixed the code per @napilnik's comment. As far as not using for-in, it doesn't change the basic idea and can be decided by the developer once he/she knows the consequence.

Lifz
  • 690
  • 6
  • 10
  • last string: arr[i].nestedArray[j].param = arr[i].name; – napilnik Jul 01 '16 at 20:43
  • A good answer should explain what's wrong with the original code and how you've fixed the problems. – Barmar Jul 01 '16 at 20:43
  • It's generally not considered a good idea to use `for-in` on arrays. http://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-such-a-bad-idea?lq=1 – Barmar Jul 01 '16 at 20:44
  • You still haven't explained what the OP's problem is and why your solution fixes it. What's wrong with using `forEach` that's solved by using `for-in`? – Barmar Jul 01 '16 at 21:06