Issue
I have this array from an API:
let passions = [
{_id: "60a1557f0b4f226732c4597c",name: "Netflix"},
{_id: "60a1557f0b4f226732c4597b",name: "Movies"}
]
I would like to return exactly the following array:
['60a1557f0b4f226732c4597c', '60a1557f0b4f226732c4597b']
My code:
for(var key in passions){
return key._id
}
Solution
A simple map function should be enough to fix this issue for you.
const extractedIds = passions.map((e) => e._id);
The .map function applies that given function (in this case the function is (e) => e.["_id"]) to each element of your array and returns an array of the results. This means that it's going to return out a new array in the way you're hoping.
Answered By - Slothyboi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.