Reputation: 41
I have this array:
array = [{key: "6S", values: { 3: [{Id: "1234a"}, {Id: "1234b"}]}},
{key: "7S", values: { 5: [{Id: "1534a"}], 4: [{Id:"1534a"}]}}]
I want to map "3", "5" and "4" (that are actually keys from another list inside the big list) from the values...something like this
array.map(function(d)
{ return d.values."key";}));
In order to access the values 3, 5 and 4 I can use a for loop. However, there is a way to access values directly?
Upvotes: 1
Views: 62
Reputation: 10148
Object.keys gives you array and as a result in map, you get something like
[[3],[4,5]]
You can flatten the return array to single array like
array.join(',').split(',');
var array = [{
key: "6S",
values: {
3: [{
Id: "1234a"
}, {
Id: "1234b"
}]
}
},
{
key: "7S",
values: {
5: [{
Id: "1534a"
}],
4: [{
Id: "1534a"
}]
}
}
];
var newArray = array.map(o=> Object.keys(o.values))
console.log(newArray.join(',').split(','))
Upvotes: 0
Reputation: 781003
Object.keys()
will return an array of the keys of an object. Then you can use concat()
to concatenate them.
array = [{key: "6S", values: { 3: [{Id: "1234a"}, {Id: "1234b"}]}},
{key: "7S", values: { 5: [{Id: "1534a"}], 4: [{Id:"1534a"}]}}];
var result = [].concat(...array.map(d => Object.keys(d.values)));
console.log(result);
Upvotes: 2