Replace element at specific position in an array without mutating it

前端 未结 7 592
感情败类
感情败类 2021-01-31 08:20

How can the following operation be done without mutating the array:

let array = [\'item1\'];
console.log(array); // [\'item1\']
array[2] = \'item2\'; // array is         


        
相关标签:
7条回答
  • 2021-01-31 09:15

    var list1 = ['a','b','c'];
    var list2 = list1.slice();
    list2.splice(2, 0, "beta", "gamma");
    console.log(list1);
    console.log(list2);

    Is this what you want?

    0 讨论(0)
提交回复
热议问题