我有一个带有对象的数组。所有对象都有一个唯一的id。现在我想在具有特定id的对象之前插入一个新对象。为此,我得到了这个对象:
{"topic":"add","payload":{"id":1,"end":"24:00","temp":21,"add":0,"delete":0},"row":-1,"socketid":"SSZY-1C3jiP8-NS2AAAB","_msgid":"a891f409.e30038"}
现在我需要在这个数组中找到这个id为的对象
[{"woche":"Montag","status":0,"_children":[{"id":1,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Dienstag","status":0,"vortag":1,"_children":[{"id":2,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Mittwoch","start":"","status":0,"vortag":1,"_children":[{"id":3,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Donnerstag","status":0,"vortag":1,"_children":[{"id":4,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Freitag","start":"","status":0,"vortag":1,"_children":[{"id":5,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Samstag","start":"","status":0,"vortag":1,"_children":[{"id":6,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Sonntag","start":"","status":0,"vortag":1,"_children":[{"id":7,"end":"24:00","temp":21,"add":0,"delete":0}]}]
现在我想在具有匹配id的对象前面插入一个新对象。
如何在数组中找到位置,以及如何添加此对象?
{"id":`${++Id}`,"end":"","temp":21,"add":0,"delete":0}
最后应该是这样的:
[{"woche":"Montag","status":0,"_children":[{"id":"8","end":"","temp":21,"add":0,"delete":0},{"id":1,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Dienstag","status":0,"vortag":1,"_children":[{"id":2,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Mittwoch","start":"","status":0,"vortag":1,"_children":[{"id":3,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Donnerstag","status":0,"vortag":1,"_children":[{"id":4,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Freitag","start":"","status":0,"vortag":1,"_children":[{"id":5,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Samstag","start":"","status":0,"vortag":1,"_children":[{"id":6,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Sonntag","start":"","status":0,"vortag":1,"_children":[{"id":7,"end":"24:00","temp":21,"add":0,"delete":0}]}]
您可以使用filter
方法来检查特定元素的索引。
假设这是您的数组:
let arr = [{"woche":"Montag","status":0,"_children":[{"id":1,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Dienstag","status":0,"vortag":1,"_children":[{"id":2,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Mittwoch","start":"","status":0,"vortag":1,"_children":[{"id":3,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Donnerstag","status":0,"vortag":1,"_children":[{"id":4,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Freitag","start":"","status":0,"vortag":1,"_children":[{"id":5,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Samstag","start":"","status":0,"vortag":1,"_children":[{"id":6,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Sonntag","start":"","status":0,"vortag":1,"_children":[{"id":7,"end":"24:00","temp":21,"add":0,"delete":0}]}]
您需要在数组中搜索此对象的索引:
let s = {"topic":"add","payload":{"id":1,"end":"24:00","temp":21,"add":0,"delete":0},"row":-1,"socketid":"SSZY-1C3jiP8-NS2AAAB","_msgid":"a891f409.e30038"}
您可以使用以下查询来实现这一点:
let index = -1
arr.filter((o) => {
if(s.payload.id === o._children[0].id) {
index = arr.indexOf(o)
}
return o
})
可以使用splice
方法在数组中的特定索引处添加新对象。
arr.splice(index, 0, <value to enter in array>)
如果您是为相对现代的web浏览器或节点应用程序编写,则可以使用flatMap
:
const newItem; // I assume this is your object you want to insert
const fullArray; // I assume this is the full array
const result = fullArray.flatMap(
item => item.id === "1" ? [newItem, item] : [item]
);
据我所知,这是获得与fullArray
相同的新阵列result
的最有效方法,除了每次在满足条件item.id === "1"
的fullArray
中出现项目之前插入newItem
。
关于flatMap
的简短解释:.flatMap(x)
是更有效的.map(x).flat()
的缩写。其思想是,例如,将[a, b, c, d]
和.map()
转换为[[a], [b], [newItem, c], [d]]
,然后将.flat()
转换为[a, b, newItem, c, d]
。尽管flatMap
这样做并没有低效地完成两个步骤,而是一次性完成,因此在效率方面没有太大的改进空间。