1

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
  • 208,517
  • 23
  • 234
  • 262
Beginner_Hello
  • 357
  • 6
  • 19

3 Answers3

0

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
  • 4,125
  • 7
  • 28
  • 33
-1

The hard way with map:

list.map(e => e.value)

With Lodash _.map it's even easier:

_.map(list, 'value')
tadman
  • 208,517
  • 23
  • 234
  • 262
-2

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
  • 81,660
  • 23
  • 145
  • 268