Skip to content
Advertisement

Ajax how to find something in array of objects via find method

In backend PHP I have this defined in loop:

JavaScript

and then final list is created:

JavaScript

In javascript I am getting these results via ajax call:

JavaScript

The first console.log(result); gives me all data something like:

JavaScript

but console.log(this.myoutput.approved); gives me undefined. Is something what I am doing wrong here?

Advertisement

Answer

The key that holds your array values is named values, not list. So this should work:

JavaScript

But what are you trying to find? If you check the values array, it contains id and title, not list? so to find one item in your array, something like:

JavaScript

would make more sense to me. Note that if nothing is found, the find() method will indeed return undefined as per the specification: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

Good luck!

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement