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?
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?
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);
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);
array1 = [{'x': 1}, {'x': 2}, {'x': 3}, {'x': 5}]
const values = array1.map(object => object.x)
console.log(values)
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)