Reputation: 81
I have a array like this:
Array[2]
0: Array[1] 0: Object
color: "d64b23"
hasta_sayisi: 84412
il: "TOKAT"
__proto__: Object
length: 1
__proto__: Array[0]
1: Array[1]
0: Object
color: "499a84"
hasta_sayisi: 123068
il: "SİVAS"
__proto__: Object
length: 1
__proto__: Array[0]
I want to get objects from this array.So output should like this:
{
color: "d64b23",
hasta_sayisi: 84412,
il: "TOKAT"
},
{
color: "499a84",
hasta_sayisi: 123068,
il: "SİVAS"
}
How can we achive this with Javascript?
Thanks
Upvotes: 0
Views: 398
Reputation: 2500
I'm sure you could figure it out since all you need to do is to iterate over the array and grab the first index of each nested array.
In any case, here's a solution using map:
var result = array.map(function(a) {
return a[0];
});
If each nested array could have multiple items, then you can use .reduce()
with an inner .map()
and .concat()
var result = array.reduce(function(res, a) {
return res.concat(a.map(Object));
}, []);
The Object
is the constructor function which will simply return the first argument it's given. It's a little shorter than passing an anonymous function.
For that matter, we could just use .concat()
with .apply()
var result = [].concat.apply([], array);
This will flatten your nested arrays out to a single dimension.
Upvotes: 1