-2

Need to find an object by Sarah owner

const dogs = [
  { weight: 22, curFood: 250, owners: ['Alice', 'Bob'] },
  { weight: 8, curFood: 200, owners: ['Matilda'] },
  { weight: 13, curFood: 275, owners: ['Sarah', 'John'] }, //need to find this string by name 
  { weight: 32, curFood: 340, owners: ['Michael'] },
];

const findeOwner = dogs.find(acc => acc.owners === 'Sarah');  

just doesnt work If I remove the square brackets, then the method will work, but this is against the rules. Would you help me?

001
  • 13,291
  • 5
  • 35
  • 66
Eoylka
  • 11
  • 2
  • 1
    The square brackets mean that the value is an `Array`. So you should not compare an array with a string. You can use [`Array.includes()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes) – Harun Yilmaz Jan 20 '23 at 21:21
  • To what "rules" are you referring? – mykaf Jan 23 '23 at 14:07

1 Answers1

0

Use the find() method to find the object in the array that has "Sarah" as an owner using includes or indexOf.

const dog = dogs.find(dog => dog.owners.includes('Sarah')); //  dog.owners.indexOf('Sarah')>-1
console.log(dog);

Result:

{ weight: 13, curFood: 275, owners: ['Sarah', 'John'] }
XMehdi01
  • 5,538
  • 2
  • 10
  • 34