1
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.

So I guess you can't pass an object as a search parameter?

2
  • 1
    You can't compare objects like that, only compare references or primitive values. Commented Feb 7, 2020 at 7:27
  • As Hao Wu commented two objects can't be compared with strict equality { fruit : 'cherries' } === { fruit : 'cherries'} will return false. So you get undefined. Commented Feb 7, 2020 at 8:28

4 Answers 4

1

Please explore Sbtree module or TyrDb module (TyrDb is a wrapper on top of Sbtree with abstracted API). These libraries let you search nested element.

Sign up to request clarification or add additional context in comments.

Comments

1

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);

Comments

0

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.

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);

Comments

0

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. Then I used JSON.stringify on selectedOption and on the elements in the new array to find a match and/or index.

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])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.