How to find element in array and return | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to find element in array and return

In this api array of objects, I need to find the post with ID #7 https://code.sololearn.com/W6vBMkE1Y0kk/?ref=app

3rd Nov 2019, 8:03 PM
Ade Bakari
Ade Bakari - avatar
6 Answers
+ 2
Here's the same code but tweaked a bit. It fetches the data using the Fetch API, then finds the object with an id of "7" using the "find" method and logs the id. https://code.sololearn.com/WgqEfw2nLJ6S/?ref=app
4th Nov 2019, 1:08 AM
Ryan
Ryan - avatar
+ 3
you can use the filter() method: https://code.sololearn.com/W9S5o6jAYXYD/#js
3rd Nov 2019, 9:04 PM
MO ELomari
+ 3
Rest api called be called by id number https://jsonplaceholder.typicode.com/posts/7
3rd Nov 2019, 11:21 PM
Calviղ
Calviղ - avatar
+ 2
I found it. It was the find() method
3rd Nov 2019, 9:41 PM
Ade Bakari
Ade Bakari - avatar
+ 1
you do linear search with an for and an if-statment inside
3rd Nov 2019, 8:20 PM
Anton Böhler
Anton Böhler - avatar
+ 1
That's exactly what I got. Dope!
4th Nov 2019, 3:27 AM
Ade Bakari
Ade Bakari - avatar