how to push an element at index 0 of an array

This question already has an answer here:

  • How to insert an item into an array at a specific index? 10 answers

  • 使用Array#unshift方法。

    var array1 = ["fruit", "vegetables"],
      array2 = [
        ["apple", "banana"],
        ["tomato"]
      ];
    
    var array3 = array2.map(function(v, i) { // iterate over the array to generate the new array
      var arr = v.slice(); // copy the array
      arr.unshift(array1[i]) // insert element at beginning 
      return arr; /// return generated array
    });
    
    console.log(array3)

    Try this Array.map() and Array.unshift() . unshift() push the data into array [0]index position.

    array1 = ["fruit","vegetables"];
    array2 = [["apple","banana"],["tomato"]]; 
    
    array2.map((a,i) =>  a.unshift(array1[i]))
    console.log(array2)
    链接地址: http://www.djcxy.com/p/29308.html

    上一篇: JavaScript数组中的对象插入问题

    下一篇: 如何在数组的索引0处推入元素