[英]For each item in a loop, how would you iterate through and return an item in a different array?
I have a an array of objects like so:我有一个像这样的对象数组:
arrOfObjs =
[{type: "flower", egg: "something"},
{type: "flower", egg: "something2"},
{type: "notFlower", egg: "something3"},
{type: "flower", egg: "something4"}]
Each object in the array has a 'type'.数组中的每个对象都有一个“类型”。 Whenever a user clicks on an item on the page, then another object is added to the array with type either "flower" or "notFlower"
每当用户点击页面上的一个项目时,就会将另一个对象添加到数组中,类型为“flower”或“notFlower”
I have another array like so:我有另一个像这样的数组:
allProperties =
["flower_style_1",
"flower_style_2",
"flower_style_3",
"flower_style_4",
"flower_style_5",
"flower_style_6",
"flower_style_7",
"flower_style_8"]
As you can tell it's essentially flower_style_ then a number up to 8.如您所知,它本质上是flower_style_,然后是最多 8 的数字。
For each item in arrOfObjs with type = flower, and any new item added, then there should be a return statement where the subject returned is something from the allProperties array.对于 arrOfObjs 中 type = flower 的每个项目,以及添加的任何新项目,都应该有一个 return 语句,其中返回的主题是 allProperties 数组中的内容。
For example, in arrOfObjs above:例如,在上面的 arrOfObjs 中:
Once all items in allProperties have been returned, then start from the beginning flower_style_1 and return that for each new item in arrOfObjs.返回 allProperties 中的所有项目后,从开始的 flower_style_1 开始,并为 arrOfObjs 中的每个新项目返回该元素。
So far, I have the following written which is a foreach loop going through arrOfObjs.到目前为止,我已经编写了以下内容,这是一个通过 arrOfObjs 的 foreach 循环。
this.arrOfObjs?.forEach(arrItem => {
if (arrItem.type === 'flower') {
this.allProperties.forEach(element => {
return element;
}
}
});
However it doesn't work and I think there would be a more efficient way of achieving this.但是它不起作用,我认为会有更有效的方法来实现这一点。 Especially without having an array of flower_style_.......
尤其是没有一个flower_style_数组......
Would anyone have an idea of how to achieve the above?有没有人知道如何实现上述目标?
arrOfObjs = [{type: "flower", egg: "something"}, {type: "flower", egg: "something2"}, {type: "notFlower", egg: "something3"}, {type: "flower", egg: "something4"}] allProperties = ["flower_style_1", "flower_style_2", "flower_style_3", "flower_style_4", "flower_style_5", "flower_style_6", "flower_style_7", "flower_style_8"]; let getArray=[]; arrOfObjs?.forEach((arrItem,i) => { if (arrItem.type === 'flower') { getArray.push(allProperties[getArray.length]); } }); console.log(getArray);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.