简体   繁体   English

在嵌套对象数组中查找对象

[英]Find object in array of nested objects

const inventory = [
  { name: { vegetable: 'carrot' }, quantity: 2 },
  { name: { meat: 'pork' }, quantity: 0 },
  { name: { fruit: 'cherries' }, quantity: 5 },
];

const result = inventory.find(name => name === { fruit: 'cherries' });
console.log(result);

I have an array of nested objects and I'm trying to find out if there is one including the { fruit: 'cherries' } object but I get undefined as a result.我有一组嵌套对象,我试图找出是否有一个包含{ fruit: 'cherries' }对象的对象,但结果我得到了未定义的对象。

So I guess you can't pass an object as a search parameter?所以我猜你不能将对象作为搜索参数传递?

You need to write full path to the object and access to object through .您需要写入对象的完整路径并通过. sign:标志:

const result = inventory.find(name => name.name.fruit === 'cherries');

An example:一个例子:

 const inventory = [ { name: { vegetable: 'carrot' }, quantity: 2 }, { name: { meat: 'pork' }, quantity: 0 }, { name: { fruit: 'cherries' }, quantity: 5 }, ]; const result = inventory.find(name => name.name.fruit === 'cherries'); console.log(result);

It will be better seen in debugger what there is actual path to your properties of object:在调试器中可以更好地看到对象属性的实际路径:

const result = inventory.find(name => {
    if(name.name.fruit === 'cherries')
        return name;
    return;
});
console.log(result);

You can't compare objects because they have different references .您无法比较对象,因为它们具有不同的引用 One way could be using destructuring and just simply access the fruit property in order to compare it with a given value .一种方法可能是使用解构,只需简单地访问fruit属性,以便将其与给定的进行比较。

 const inventory = [ { name: { vegetable: 'carrot' }, quantity: 2 }, { name: { meat: 'pork' }, quantity: 0 }, { name: { fruit: 'cherries' }, quantity: 5 }, ]; const result = inventory.find(({name}) => name.fruit == "cherries"); console.log(result);

Please explore Sbtree module or TyrDb module (TyrDb is a wrapper on top of Sbtree with abstracted API).请探索Sbtree模块或TyrDb模块(TyrDb 是在 Sbtree 之上的具有抽象 API 的包装器)。 These libraries let you search nested element.这些库可让您搜索嵌套元素。

Thanks for all the suggestions.感谢所有的建议。 I think I found a solution to my problem.我想我找到了解决问题的方法。

I used array.map to create a new array consisting of only the name objects .我使用array.map创建了一个仅包含名称 objects的新数组。 Then I used JSON.stringify on selectedOption and on the elements in the new array to find a match and/or index.然后我在selectedOption和新数组中的元素上使用JSON.stringify来查找匹配项和/或索引。

There is probably a better way of doing it but this is working OK.可能有更好的方法来做到这一点,但这工作正常。 Feel free to point out any potential flaws in my approach.随时指出我方法中的任何潜在缺陷。

 const selectedOption = {fruit_red: "cherries", fruit_green: "kiwi"} const inventory = [ { name: { vegetable: 'carrot' }, quantity: 2 }, { name: { meat: 'pork' }, quantity: 0 }, { name: { fruit_red: 'cherries', fruit_green: 'kiwi' }, quantity: 5 }, { name: { fruit_red: 'cherries', fruit_green: 'apple' }, quantity: 3 }, ]; const names = inventory.map(item => item.name) const resultIndex = names.findIndex(name => JSON.stringify(name) === JSON.stringify(selectedOption)); console.log(resultIndex) const result = names.find(name => JSON.stringify(name) === JSON.stringify(selectedOption)); console.log(result) console.log(inventory[resultIndex])

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM