[英]Insert Object in Array before other object in array with specific property
I have one Array with objects.我有一个带有对象的数组。 All objects habe one unique id.
所有对象都有一个唯一的 ID。 Now I want to insert a new Object before the object with the specific id.
现在我想在具有特定 ID 的 object 之前插入一个新的 Object。 To do that I get this object:
为此,我得到了这个 object:
{"topic":"add","payload":{"id":1,"end":"24:00","temp":21,"add":0,"delete":0},"row":-1,"socketid":"SSZY-1C3jiP8-NS2AAAB","_msgid":"a891f409.e30038"}
and now I need to find this object in this array with the id:现在我需要在这个数组中使用 id 找到这个 object:
[{"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}]}]
and now I want to insert a new object bofore the object with the matching id.现在我想在具有匹配 ID 的 object 之前插入一个新的 object。
How can I find the position in the array and how can I add this object?如何在数组中找到 position 以及如何添加这个 object?
{"id":`${++Id}`,"end":"","temp":21,"add":0,"delete":0}
At the end it should look like this:最后它应该是这样的:
[{"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}]}]
You can use filter
method to check for the index of the specific element.您可以使用
filter
方法来检查特定元素的索引。
Suppose this is your array:假设这是您的数组:
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}]}]
You need to search for the index of this object in the array:需要在数组中搜索这个 object 的索引:
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"}
You can use the following query to achieve this:您可以使用以下查询来实现此目的:
let index = -1
arr.filter((o) => {
if(s.payload.id === o._children[0].id) {
index = arr.indexOf(o)
}
return o
})
You can use splice
method to add new object in the array at specific index.您可以使用
splice
方法在特定索引处的数组中添加新的 object。
arr.splice(index, 0, <value to enter in array>)
You can use flatMap
if you are writing for relatively modern web browsers or a node application:如果您正在为相对现代的 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]
);
This is to my knowledge the most efficient way to get a new array result
that is identical to fullArray
except with newItem
inserted every time before an item occurs in fullArray
that fulfills the condition item.id === "1"
.据我所知,这是获得与
fullArray
相同的新数组result
的最有效方法,除了每次在fullArray
中出现满足条件item.id === "1"
的项目之前插入newItem
。
A short explanation about flatMap
: .flatMap(x)
is an abbreviation for .map(x).flat()
that is more efficient.关于
flatMap
的简短解释: .flatMap(x)
是.map(x).flat()
的缩写,效率更高。 The idea is that you have eg [a, b, c, d]
and .map()
it to [[a], [b], [newItem, c], [d]]
which is then .flat()
tened to [a, b, newItem, c, d]
.这个想法是你有例如
[a, b, c, d]
和.map()
它到[[a], [b], [newItem, c], [d]]
然后.flat()
到[a, b, newItem, c, d]
。 Although flatMap
does this without inefficiently going through both steps but instead does it in one go, so there is not much space for improvement efficiency wise.尽管
flatMap
这样做并没有低效地经历这两个步骤,而是在一个 go 中完成,因此在提高效率方面没有太多空间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.