将数组插入到数组Javascript中

时间:2018-03-06 04:36:58

标签: javascript jquery arrays multidimensional-array

我尝试过拼接,连接,申请,电话等等。

我想做的就是采用这样的数组:

[["alksjdflskdj","2","1.33","1.30","", "5","1","1","1","1","1","0","","Other notes"],
 ["test","1","","","","1","0","1","0","0","0","",""],
 ["test3","","","","","","0","0","0","0","0","",""],
 ["","","","","","","0","0","0","0","0","",""],
 ["","","","","","","0","0","0","0","0","",""]]

并在上面的数组中添加这样的内容:

["2018-03-06 04:14:59", "T", "", "", "0"]

最终结果如下:

[["alksjdflskdj","2","1.33","1.30","", "5","1","1","1","1","1","0","","Other notes"],
 ["test","1","","","","1","0","1","0","0","0","",""],
 ["test3","","","","","","0","0","0","0","0","",""],
 ["","","","","","","0","0","0","0","0","",""],
 ["","","","","","","0","0","0","0","0","",""],
 ["2018-03-06 04:14:59", "T", "", "", "0"]]

提前致谢。这让我很难过。

6 个答案:

答案 0 :(得分:2)

您可以使用其他人已经提到的push(),以便向现有数组添加元素:

arr1.push(ele); // arr1 now has ele at last index

您可以使用concat(),它不会改变原始array并返回一个全新的:{/ p>

var arr2 = arr1.concat(ele) // arr1 remains the same and arr2 has arr1 with ele at las index

这取决于您的需求。

答案 1 :(得分:1)

使用push方法将元素添加到数组的末尾。



var originalArray = [
  ["alksjdflskdj", "2", "1.33", "1.30", "", "5", "1", "1", "1", "1", "1", "0", "", "Other notes"],
  ["test", "1", "", "", "", "1", "0", "1", "0", "0", "0", "", ""],
  ["test3", "", "", "", "", "", "0", "0", "0", "0", "0", "", ""],
  ["", "", "", "", "", "", "0", "0", "0", "0", "0", "", ""],
  ["", "", "", "", "", "", "0", "0", "0", "0", "0", "", ""]
]

var ArrayToPush = ["2018-03-06 04:14:59", "T", "", "", "0"];

originalArray.push(ArrayToPush);
console.log(originalArray)




答案 2 :(得分:1)

您需要使用push()方法来实现目标。这是一个片段:

var initial = [
    ["alksjdflskdj","2","1.33","1.30","", "5","1","1","1","1","1","0","","Other notes"],
    ["test","1","","","","1","0","1","0","0","0","",""],
    ["test3","","","","","","0","0","0","0","0","",""],
    ["","","","","","","0","0","0","0","0","",""],
    ["","","","","","","0","0","0","0","0","",""]
];

var new_array = ["2018-03-06 04:14:59", "T", "", "", "0"];
initial.push(new_array);

initial现在包含您正在寻找的新阵列数组。

答案 3 :(得分:1)

您可以使用array#push()向数组中添加新元素。

var data = [["alksjdflskdj","2","1.33","1.30","", "5","1","1","1","1","1","0","","Other notes"],["test","1","","","","1","0","1","0","0","0","",""],["test3","","","","","","0","0","0","0","0","",""],["","","","","","","0","0","0","0","0","",""],["","","","","","","0","0","0","0","0","",""]],
    arr = ["2018-03-06 04:14:59", "T", "", "", "0"];
data.push(arr);
console.log(data);

答案 4 :(得分:1)

您可以尝试使用Array.push()这样的

var datas = [["alksjdflskdj","2","1.33","1.30","", "5","1","1","1","1","1","0","","Other notes"],["test","1","","","","1","0","1","0","0","0","",""],["test3","","","","","","0","0","0","0","0","",""],["","","","","","","0","0","0","0","0","",""],["","","","","","","0","0","0","0","0","",""]];

然后

datas.push(["2018-03-06 04:14:59", "T", "", "", "0"])

答案 5 :(得分:0)

将要推送的元素存储在变量

使用arr.push(ele);

{{1}}

这将自动增加索引并将其插入到最后一个