Reputation:
Currently I'm getting data in the format below:
arr=[
0: {abc:1},
1: {efg:2},
2: {hij:3}
]
I need it in below format:
arr=[
{name:'abc', value:1},
{name:'efg', value:2},
{name:'hij', value:3}]
Upvotes: 3
Views: 10911
Reputation:
var arr = [ {abc:1}, {efg:2},{hij:3}],
var arr1=[];
_.forEach(arr, function (val, key) {
_.forIn(val, function (val, key) {
arr1.push({ name: key, value: val });
})
});
console.log(arr1);
});**Implemented using loadash**
Upvotes: 0
Reputation: 1712
Assuming an arr
has a following structure, it's only a matter of mapping through it and separating key and value:
var arr = [
{abc:1},
{efg:2},
{hij:3}
]
var result = arr.map(o => {
var k = Object.keys(o)[0];
return {
name: k,
value: o[k]
};
});
console.log(result);
Upvotes: 4