0

I've done a few apollo queries in react and I'm trying to run an includes check against them. It however it always comes up false in the console log. Here's my code:

const onSubmit = (data: any) => {
console.log (data1)
console.log(data2)
console.log(data2.item)
console.log([data1].includes(data2.item))}

and here's the output:

console.log(data1): Test Array(2)
0: {Name: 'TEST', item: 'Crayons'}
1: {Name: 'TEST', item: 'Taco'} 
console.log(data2) : {Name: 'Jack', place: 'Home', item: 'Taco'}
console.log(data2.item) : Taco
console.log([data1].includes(data2.item)) : False

I'm looking for the output of that second column 'item' where taco matches to come up as true.

I've also tried accessing by data1.item[1].item but it still returned a false.

It may be because the type is an object?

Is there a simple way to check the entire second column of data1? (item). The list only gets bigger

  • 1
    Please update your tags to include `javascript` and remove the `reactjs` as this isn't related to that library. – John Jul 06 '22 at 21:24

1 Answers1

0

Include will check to see if the objects are the same by reference value. You can check if an entry's item is matching by using some.

console.log(data1.some(d => d.item === data2.item));
John
  • 1,240
  • 9
  • 13
  • Thank you for this! Im new to js and didn't know some() was a thing. I've looked into it and it seems like it could work. Out of curiosity would there be a way to make these reference values the same and still use include? –  Jul 07 '22 at 02:34
  • 1
    Take a look at this answer to get a better understanding of what I mean by reference https://stackoverflow.com/a/6605700/3657537 – John Jul 08 '22 at 09:17