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}]
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}]
Share
Improve this question
edited Jun 19, 2018 at 21:25
Mike Williamson
3,28016 gold badges77 silver badges119 bronze badges
asked Jun 19, 2018 at 21:17
user5553165user5553165
3
- 1 so start looping and convert it. developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – epascarello Commented Jun 19, 2018 at 21:19
- This question is lacking content and understanding. It needs to be clarified before it can be answered. Think more in terms of WHAT you want to do, not what should e out of it. Do you want to access the values in the objects but you don't know the keys? – Robert Mennell Commented Jun 19, 2018 at 21:21
- 2 Also another question: What happens if you have more than one key and value per object? – Robert Mennell Commented Jun 19, 2018 at 21:34
2 Answers
Reset to default 4Assuming 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);
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**