简体   繁体   English

数组有 5 个对象,每个对象都有另外 5 个对象的数组属性。 我正在尝试使用 ID 查找对象的函数

[英]Array has 5 objects and each object has an array property of another 5 objects. I'm trying to make function that finds the object with ID

First of all, this is the JSON file which is used - https://ddragon.leagueoflegends.com/cdn/9.14.1/data/en_US/runesReforged.json首先,这是使用的 JSON 文件 - https://ddragon.leagueoflegends.com/cdn/9.14.1/data/en_US/runesReforged.json

Currently I have this function that allows me to put a specific id as an argument and get one of the 5 main objects in that array:目前我有这个函数,它允许我将一个特定的 id 作为参数并获取该数组中的 5 个主要对象之一:

findSummonerRune: (state) => (id) => {
    let rune = state.summonerRunes.find(rune => rune.id == id);

    return rune
}

So findSummonerRune(8100) returns the first object with "key": "Domination" property.所以findSummonerRune(8100)返回第一个具有"key": "Domination"属性的对象。 However, if I try to find rune with id of 8112 , it doesn't work because that rune is a sub-rune which is found in the properties of rune with id of 8100 .但是,如果我尝试查找 id 为8112符文,它不起作用,因为该符文是一个子符文,可在 id 为8100的符文属性中找到。 More specifically, in the slots property.更具体地说,在slots属性中。

Now I understand why my function doesn't work, it's made to find which one of the main 5 objects has a property id == id, however, I'm just not sure how to also loop through their array property slots to look for the other runes as well.现在我明白为什么我的函数不起作用了,它是用来查找主要 5 个对象中的哪一个具有属性 id == id,但是,我只是不确定如何循环遍历它们的数组属性槽来查找其他符文也是如此。

personally, if you want to return the item from slots , I'd flatten my list once upfront:就个人而言,如果您想从slots返回该项目,我会预先将我的列表展平:

state.flattenedRunes = state.summonerRunes.reduce((acc, rune) => 
                         acc.concat([rune], rune.slots.reduce((a, r) => a.concat(r.runes), [])), []);

and I'd just search that list instead而我只是搜索该列表

if you want to actually return the main object that contains the item in its slots , then do this to also check the sub arrays on your find.如果您想实际返回在其slots中包含该项目的主要对象,则执行此操作以检查您的发现中的子数组。

let rune = state.summonerRunes.find(rune => rune.id == id || rune.slots.find(slot => slot.runes.find(r => r.id == id)));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 得到一个包含 2 个对象的数组。每个对象都有一个属性 `events`,其中包含一个对象数组。 如何过滤参与者ID不为1的元素? - Got an array of 2 objects.Each object has a property `events` which contains an array of objects. How to filter elements whose participantId is not 1? 如何检查包含另一个对象数组的对象数组是否具有属性 - How to check if array of objects that contains another array of object has property 按每个对象具有的属性对对象数组进行排序时,结果异常 - Unusual results when sorting an array of objects by a property each object has 如果对象具有属性,则对对象数组进行排序 - sort array of objects if the object has a property 无法发送具有对象数组的属性的对象 - Cant send Object with property that has array of objects 比较2个对象数组。 按 id 匹配,将对象属性推入数组 - compare 2 array of objects. match by ids, push object property into array 对象数组的对象。 获取具有相同属性的所有值的长度 - Object of array of objects. Get the length of all values with the same property JavaScript获得对象数组的值或属性(如果对象具有该属性) - JavaScript get value or property of an Array of objects if object has that property Mongoose 检索对象数组。 对于每个对象,我只想要两个属性 - Mongoose retrieve array of objects. For each object, I only want two properties 如何过滤对象具有另一个数组的对象数组? - How to filter array of objects where object has another array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM