-4

If I've got

var aaa = [
 {"id": 1, "text": "Ann"},
 {"id": 2, "text": "Bob"},
 {"id": 3, "text": "Carol"},
 {"id": 4, "text": "Carol"},
]

and I want to get all the elements that text "Carol" but specifically I want just their IDs.What do I do?

Imbro
  • 479
  • 4
  • 4
  • 1
    Use [`.filter`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) and [`.map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter). See also [From an array of objects, extract value of a property as array](https://stackoverflow.com/q/19590865/218196) – Felix Kling Sep 07 '20 at 10:49
  • 1
    Use a for loop and if the object's `text` is `"Carol"`, push its `id` to an output array – adiga Sep 07 '20 at 10:51

1 Answers1

0

Try

let idList = aaa.filter(a => a.text == 'Carol').map(({ id }) => id);
Dalibor
  • 1,430
  • 18
  • 42