How to append new array to beginning of multi

This question already has an answer here:

  • How can I add new array elements at the beginning of an array in JavaScript? 6 answers

  • 在这里,您可以使用解决方案https://jsfiddle.net/fkz9ubug/

    var vArr = [[1, 1], [2, 3], [3, 3]];
    var newv = [4, 4];
    vArr.unshift(newv)
    
    console.log(vArr);

    The problem is with assigning the result of vArr.splice(0, 0, newv) back to vArr .

    The splice function can also remove items from the array and the return value of splice() is those removed items.

    So vArr = vArr.splice(0, 0, newv); should simply be vArr.splice(0, 0, newv); .


    您可以使用unshift()在原始数组的开头推送新数组,并使用pop()从数组中移除最后一个元素:

    var vArr = [[1, 1], [2, 2], [3, 3]];
    var arrToPush = [4, 4];
    vArr.unshift(arrToPush);
    vArr.pop();
    console.log(vArr);
    链接地址: http://www.djcxy.com/p/19362.html

    上一篇: 如何在javascript中合并2个对象

    下一篇: 如何将新数组添加到多个开头