Replace element at specific position in an array without mutating it

前端 未结 7 618
感情败类
感情败类 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?

提交回复
热议问题