How to insert a new element in between all elements of a JS array?

后端 未结 17 1271
清歌不尽
清歌不尽 2020-12-29 20:38

I have an array [a, b, c]. I want to be able to insert a value between each elements of this array like that: [0, a, 0, b, 0, c, 0].

I gues

17条回答
  •  不知归路
    2020-12-29 21:03

    all of the above methods in very long strings made my android computer run on React Native go out of memory. I got it to work with this

    let arr = ['a', 'b', 'c'];
    let tmpArr = [];
    
    for (const item in arr) {
      tmpArr.push(item);
      tmpArr.push(0);
    }
    
    console.log(tmpArr);

提交回复
热议问题