-1

how do I find and return a specific object key and return as array in lodash.

const category = [
 { id: 123, name: "haha"},
 { id: 124, name: "haha2"},
 { id: 125, name: "haha3"},
]

how do i get this? result: [123,124,125]

phongyewtong
  • 5,085
  • 13
  • 56
  • 81
  • 1
    Does this answer your question? [Filtering array of objects with lodash based on property value](https://stackoverflow.com/questions/35182904/filtering-array-of-objects-with-lodash-based-on-property-value) – Doruk Eren Aktaş Jun 08 '20 at 17:17

2 Answers2

0

make for each loop to iterate over category and each element of category has id push it into empty arr

let result = []

const category = [
    { id: 123, name: "haha"},
    { id: 124, name: "haha2"},
    { id: 125, name: "haha3"},
   ]

category.forEach(c=>{
    result.push(c.id)
})

console.log(result)
Ahmed Magdy
  • 1,054
  • 11
  • 16
-1

You can use Array.map:

category.map(item => item.id)

Ján Jakub Naništa
  • 1,880
  • 11
  • 12