1
let nestedArr = [[0,0],[0,0]]
let insideArr = nestedArr[0]
let targetArr = [1,1]

Basically I want to change the insideArr's elements to equal to targetArr, and it should change the first element of nestedArr too (because insideArr is refering to that).

insideArr = targetArr
insideArr = [..targetArr]

Above 2 approaches won't work because it will make insideArr pointing to new reference. I know using forEach to loop through insideArr and assign it one by one should work, but is there a better way? And BTW, should I avoid this kind of usage?

Spinik Lee
  • 23
  • 3
  • 1
    `avoid this kind of usage` for what? – ksav May 25 '21 at 01:38
  • 1
    Why not `nestedArr[0] = targetArr`? Why do you need to maintain `insideArr` as a reference to `nestedArr[0]`? – Phil May 25 '21 at 01:39
  • 1
    Got to say the close votes are ridiculously over-zealous. It's a perfectly valid question. – see sharper May 25 '21 at 01:46
  • Related: [Replace everything inside an Array with a new value](https://stackoverflow.com/q/17945494/1048572), [Is there a better way to retain your array but efficiently concat or replace items?](https://stackoverflow.com/q/19672848/1048572) – Bergi May 25 '21 at 01:56
  • @ksav That's what I'm asking, I don't if this kind of editing will make the code less robust. – Spinik Lee May 25 '21 at 22:57

2 Answers2

3

If you must maintain insideArr as a reference to nestedArr[0], you can use Array.prototype.splice() to mutate insideArr

let nestedArr = [[0,0],[0,0]]
let insideArr = nestedArr[0]
let targetArr = [1,1]

// Replace all of insideArr with targetArr
insideArr.splice(0, insideArr.length, ...targetArr)

console.log("still the same reference?", insideArr === nestedArr[0])
console.log("insideArr:", insideArr)
console.log("nestedArr:", nestedArr)
.as-console-wrapper { max-height: 100% !important; }
Phil
  • 157,677
  • 23
  • 242
  • 245
2

Assuming you have good reasons for keeping the reference the same - and sometimes there are, you can do:

let nestedArr = [
  [0, 0],
  [0, 0]
];
let insideArr = nestedArr[0];
let targetArr = [1, 1];
insideArr.length = 0;
insideArr.unshift(...targetArr); // insideArr.push(...targetArr) works fine too
console.log(nestedArr);
see sharper
  • 11,505
  • 8
  • 46
  • 65
  • I prefer Phil's one-line answer using splice, but keeping this here to show there's more than one way to skin a cat. – see sharper May 25 '21 at 01:45
  • 1
    Love the old `.length = 0` trick. You could also use `push` instead of `unshift` (just because it's a more commonly understood method) – Phil May 25 '21 at 01:46
  • @Phil Honestly my first guess would've been that `unshift` adds them in the wrong order – Bergi May 25 '21 at 01:52
  • @Bergi same. I had to check the docs ~ [_"if multiple elements are passed as parameters, they're inserted in chunk at the beginning of the object, in the exact same order they were passed as parameters"_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift) – Phil May 25 '21 at 02:19
  • Good point re being less well known, and honestly I'm not sure why I chose it over push! Added comment to code. – see sharper May 25 '21 at 02:35