Hacking to Win
Hacking to Win

Reputation: 55

How to find specific object in node js

So I have run my code and here is the output

{ data:
   { cards:
      [ [Object],
        [Object],
        [Object],
        [Object],
        [Object] ]

Then I run these

const getdata = data.cards[0]

and the result is

Tester I'm the value you looking for

Problem is, these object is always changed.

I don't know where the object I want to looking for. They can be on cards[0] or cards[1] or cards[2] etc.

Because when I try to run same code:

const getdata = data.cards[0]

The result sometimes will different, they can be:

Sorry, this is not value you looking for

So. Is it possible to find spesific object with value or something like that?

The value I'm looking for is exactly on these

data.cards[????].content.actions[0].deep_link

The outpot will be:

Im the value

Upvotes: 0

Views: 156

Answers (1)

Davo
Davo

Reputation: 986

you can use find or findIndex on the data.cards array, like this:

To return the object:

const found = data.cards.find( d => d.content.actions[0].deep_link === 'YOUR_VALUE' )

To return the index:

const foundIndex = data.cards.findIndex( d => d.content.actions[0].deep_link === 'YOUR_VALUE' )

Where d represents each element in the array. Then, you need to make your validation and if it returns true; find or findIndex will return the object or index; on the other hand, if your returns false; the function will move on on the next element of the array.

Hope this helps :)

Upvotes: 2

Related Questions