Reputation: 233
I am getting http response like this:
[
{"id": "1", "name": "2", "value": "3", "any": "4"}
]
I want to convert it to something like this:
[
{
"heading": "id"
"content": "1"
},
{
"heading": "name"
"content": 2
},
{
"heading": "value"
"content": 3
},
{
"heading": "any"
"content": 4
}
]
I am using angular4.0.0 and I want to perform this in service method. How to achieve this result?
Upvotes: 0
Views: 201
Reputation: 1138
var responseData=[
{"id": "1", "name": "2", "value": "3", "any": "4"}
];
var finalResult = [];
responseData.map(function(item){
var test = [];
var allKeys=Object.keys(item);
for(i=0;i<allKeys.length;i++)
{
finalResult.push({'heading':allKeys[i],'content':item[allKeys[i]]});
}
});
console.log(finalResult)
Upvotes: 1
Reputation: 12103
var response = [
{"id": "1", "name": "2", "value": "3", "any": "4"}
];
var newJson = [];
response.forEach(function(val,index){
Object.keys(val).forEach(function(data) {
newJson.push({heading: data,content:val[data]})
})
console.log(newJson)
})
Upvotes: 1
Reputation: 58573
Here you go :
var arrayData = [
{"id": "1", "name": "2", "value": "3", "any": "4"}
]
let finalArray = arrayData.map(el => {
let returnArray = [];
for(let key in el){
returnArray.push({heading : key , content : el[key]})
}
return returnArray;
})
console.log(finalArray);
Upvotes: 1