Reputation: 105
I need to convert the array objects to an array of arrays with its object key of each item of objects as below
var inputData = [{"0":420,"10":373,"20":340,"30":313,"40":293,"50":273,"60":259,"70":243},
{"0":620,"10":550,"20":500,"30":460,"40":430,"50":400,"60":378,"70":355},
{"0":820,"10":727,"20":660,"30":607,"40":567,"50":527,"60":497,"70":467}]
The above input data of the object has a set of values with keys from which the output array should be like below.
var outputData = [[
[0,420],[10,373],[20,340],[30,313],[40,293],[50,273],[60,259],[70,243]
],
[
[0,620],[10,550],[20,500],[30,460],[40,430],[50,400],[60,378],[70,355]
],
[
[0,820],[10,727],[20,660],[30,607],[40,567],[50,527],[60,497],[70,467]
]]
for which I had written a for each loop as below code which is returning an entire value in a single array
var inputData = [{"0":420,"10":373,"20":340,"30":313,"40":293,"50":273,"60":259,"70":243}, {"0":620,"10":550,"20":500,"30":460,"40":430,"50":400,"60":378,"70":355}, {"0":820,"10":727,"20":660,"30":607,"40":567,"50":527,"60":497,"70":467}];
var sp = Object.keys(inputData[0])
let outputData = []
inputData.forEach(i => {
sp.forEach(element => {
outputData.push([parseInt(element), i[element]])
});
})
console.log(outputData)
Upvotes: 0
Views: 54
Reputation: 1074238
In modern environments with Object.entries
(easily polyfilled), this is surprisingly simple: You use map
and use Object.entries
on each object; then convert the key to a number:
const result = inputData.map(obj => Object.entries(obj).map(([key, value]) => [+key, value]));
Live Example:
var inputData = [{"0":420,"10":373,"20":340,"30":313,"40":293,"50":273,"60":259,"70":243},
{"0":620,"10":550,"20":500,"30":460,"40":430,"50":400,"60":378,"70":355},
{"0":820,"10":727,"20":660,"30":607,"40":567,"50":527,"60":497,"70":467}];
const result = inputData.map(obj => Object.entries(obj).map(([key, value]) => [+key, value]));
console.log(result);
Note that using the unary +
is just one way of converting strings to numbers. In my answer here I go through your various options with their pros and cons.
Upvotes: 2
Reputation: 10193
You can use Object.entries
to generate [key, value]
pairs from object.
var inputData = [{"0":420,"10":373,"20":340,"30":313,"40":293,"50":273,"60":259,"70":243}, {"0":620,"10":550,"20":500,"30":460,"40":430,"50":400,"60":378,"70":355}, {"0":820,"10":727,"20":660,"30":607,"40":567,"50":527,"60":497,"70":467}];
const output = inputData.map((item) => Object.entries(item)).map((item) => item.map(([key, value]) => ([+key, +value])));
console.log(output);
Upvotes: 1
Reputation: 386560
You could map the entries of the objects and convert all values to number.
var inputData = [{ 0: 420, 10: 373, 20: 340, 30: 313, 40: 293, 50: 273, 60: 259, 70: 243 }, { 0: 620, 10: 550, 20: 500, 30: 460, 40: 430, 50: 400, 60: 378, 70: 355 },{ 0: 820, 10: 727, 20: 660, 30: 607, 40: 567, 50: 527, 60: 497, 70: 467 }],
result = inputData.map(o => Object.entries(o).map(a => a.map(Number)));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Upvotes: 2