1

I have:

array1 = [{'x': 1}, {'x': 2}, {'x': 3}, {'x': 5}] 

And I want to parse the number values after the : into this array like:

[1,2,3,5]

How would I go about doing this?

Phil
  • 7,065
  • 8
  • 49
  • 91
DuckyBluff
  • 13
  • 3

4 Answers4

1

Use the array.map function to create a new array.

array1 = [{'x': 1}, {'x': 2}, {'x': 3}, {'x': 5}] 
array2 = array1.map(o=>o.x);
console.log(array2);
I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116
1

You can use map for your requirement

let result = array1.map(c=>c.x);

array1 = [{'x': 1}, {'x': 2}, {'x': 3}, {'x': 5}] 

let result = array1.map(c=>c.x);

console.log(result);
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
0
array1 = [{'x': 1}, {'x': 2}, {'x': 3}, {'x': 5}] 

const values = array1.map(object => object.x)

console.log(values)
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
Gene Rally
  • 19
  • 3
  • 3
    Consider adding some commentary to your post to assist readers on what you are attempting to present with your code. – Milo Jun 21 '19 at 18:27
0

using Plain JS,

array1 = [{'x': 1}, {'x': 2}, {'x': 3}, {'x': 5}] 
out = []
for (i in array1) {
  out.push(array1[i]['x'])
}
console.log(out)
// using array map
op = array1.map(i => i.x)
console.log(op)
Mohideen bin Mohammed
  • 18,813
  • 10
  • 112
  • 118