Reputation: 1515
In my angular app, I get the values from service as an array of objects like below.
temp= [
{
"a": "AAA",
"b": "bbbb",
"c": "CCCC",
"d": "ddddd",
},
{
"a": "lmn",
"b": "opq",
"c": "rst",
"d": "uvw",
}
]
I need to format this temp to array of array of strings:
newTemp =
[
['AAA', 'bbbb', 'CCCC', 'ddddd'],
['lmn', 'opq', 'rst', 'uvw'],
];
Should we need to do a forloop on each object or is there any straight forward way.
Upvotes: 0
Views: 69
Reputation: 104775
You can use Array.map
let newArray = arr.map(a => Object.values(a))
If you cant use Object.values
let newArray = arr.map(a => Object.keys(a).map(k => a[k]))
Output
(2) [Array(4), Array(4)]
0:(4) ["AAA", "bbbb", "CCCC", "ddddd"]
1:(4) ["lmn", "opq", "rst", "uvw"]
Upvotes: 2
Reputation: 8239
Try the following :
temp= [
{
"a": "AAA",
"b": "bbbb",
"c": "CCCC",
"d": "ddddd",
},
{
"a": "lmn",
"b": "opq",
"c": "rst",
"d": "uvw",
}
]
var newTemp = [];
temp.forEach(function(obj){
var arr = [];
Object.keys(obj).forEach(function(key){
arr.push(obj[key]);
})
newTemp.push(arr);
});
console.log(newTemp);
Upvotes: 0