0

I have an array of objects:

[{"id":2, "value": "Yellow", "property_id": 3, "created_at": "2019-21-10"},{"id":3, "value": "Blue", "property_id": 3, "created_at": "2019-21-10"},{"id":4, "value": "White", "property_id": 3, "created_at": "2019-21-10"},{"id":12, "value": "Green", "property_id": 3, "created_at": "2019-21-10"}]

Which is generated through this line of code:

allColours.find(colr => colr == this.templateColours.colour_id).colour_default_values

In each object I want to isolate all values under 'value' e.g. the end result should be: [Yellow, Blue, White, Green].

How can I achieve this?

tadman
  • 200,744
  • 21
  • 223
  • 248
Beginner_Hello
  • 233
  • 5
  • 16

3 Answers3

-1

const arr = [{"id":2, "value": "Yellow", "property_id": 3, "created_at": "2019-21-10"},{"id":3, "value": "Blue", "property_id": 3, "created_at": "2019-21-10"},{"id":4, "value": "White", "property_id": 3, "created_at": "2019-21-10"},{"id":12, "value": "Green", "property_id": 3, "created_at": "2019-21-10"}];

const mappedArr = arr.map(item => item.value);
console.log(mappedArr);
Pat Murray
  • 3,825
  • 6
  • 25
  • 32
-2

The hard way with map:

list.map(e => e.value)

With Lodash _.map it's even easier:

_.map(list, 'value')
tadman
  • 200,744
  • 21
  • 223
  • 248
-3

The easiest way is to create a new array using map():

const arr = [{"id":2, "value": "Yellow", "property_id": 3, "created_at": "2019-21-10"},{"id":3, "value": "Blue", "property_id": 3, "created_at": "2019-21-10"},{"id":4, "value": "White", "property_id": 3, "created_at": "2019-21-10"},{"id":12, "value": "Green", "property_id": 3, "created_at": "2019-21-10"}];
const colors = arr.map(o => o.value);
Code-Apprentice
  • 76,639
  • 19
  • 130
  • 241