提问者:小点点

数组内部的JS数组-添加一个新的微型数组


在Array-JavaScript中嵌入的数组中添加元素我有以下数组:

[{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]

,在这个数组中,每个数组看起来都是这样的:

0 : {_id: "bla", header: "test", time: "test PM", content: "test", uniqueid: "test"}
    1: {_id: "blay", header: "tests", time: "tests PM", content: "even more tests", uniqueid: "tests"}
    2: {_id: "awa", header: "sd", time: 3:14:15 PM", content: "sdf", uniqueid: "sdfg"}

我的问题是,如何在这个大数组中插入另一个“微型数组”,在位置0处,头:finaltest,时间:finaltest PM,内容:thefinaltest和uniqueID:testt?

我在想也许用。不换档,但我不确定。


共2个答案

匿名用户

如果要创建一个新数组而不是改变现有数组,可以使用扩展运算符:

const originalArray = [{}, {}, {}, {}]
const newObj = { some: 'values' }

const newArray = [newObj, ...originalArray]

匿名用户

使用array.unshift()似乎是将它添加到0位置的方法。

如果要将它添加到数组的末尾,请使用array.push()

要将其添加到特定位置,Array.splice()将起作用。