简体   繁体   English

对于循环中的每个项目,您将如何迭代并返回不同数组中的项目?

[英]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 中:

  1. For {type: "flower", egg: "something"} => return "flower_style_1".对于 {type: "flower", egg: "something"} => 返回 "flower_style_1"。
  2. For {type: "flower", egg: "something2"} => return "flower_style_2".对于 {type: "flower", egg: "something2"} => 返回 "flower_style_2"。
  3. Don't return anything for {type: "notFlower", egg: "something3"}不要为 {type: "notFlower", egg: "something3"} 返回任何东西
    because it's not of "type": flower.因为它不是“类型”:花。
  4. For {type: "flower", egg: "something4"} => return "flower_style_3".对于 {type: "flower", egg: "something4"} => 返回 "flower_style_3"。
  5. etc.等等。

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.

相关问题 遍历数组并为每个项目返回一个`Dropdown.Item`元素 - Loop through the array and return a `Dropdown.Item` element for each item Javascript:如何遍历数组,每次将最后一项移动到第一项 - Javascript: How to Iterate Through An Array, Moving Last Item to First Item Each Time 如何使循环遍历枚举的每个项目并验证每个重复是否在正确的枚举中 - how to make the loop iterate through each item of an enum and validate each repetition if it is in the correct enum 如何循环遍历数组以等待每个项目之间的时间? - How to loop through an array in waiting a time between each item? 遍历每个项目的所有数组项目 - loop through all array items for each item 当在Angularjs中添加项时,如何遍历数组? - How to iterate through array as item is added in Angularjs? 是否可以遍历数组中的每个项目并输出为HTML? - Is it possible to iterate through each item in an array and output as HTML? 循环遍历多维数组以迭代javascript中的每个项目 - Looping Through Multidimensional Array to iterate each item in javascript 当循环遇到某些元素时,您将如何遍历数组,递增全局 scope 中的变量? - how would you iterate through an array, incrementing a variable in the global scope, when the loop hits certain elements? 遍历数组,将每个项目添加到对象中,然后将其推入Javascript中的数组 - Loop through an Array, add each item to an object and push it to an array in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM