Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Iterate through an array of unknown properties to find a specific value in an object

I’m trying to iterate through an array of unknown properties to find a value that exists within a JSON object although there can exist many objects as well, how can I approach this?

  args1 = ['string', 5, 3, { value: 'value', number: 2, token: 'token' }];
  args2 = [
    'string',
    { token: 'token', name: 'name' },
    1,
    3,
    2,
    { book: 'book' },
  ];

How would i be able to find the property token regardless where it exists like the two examples above?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

May be something like this:

let args1 = ['string', 5, 3, { value: 'value', number: 2, token: 'token' }];

args1.filter(x=> typeof x==='object').find(y => console.log(y.token));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading