0

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)

1
  • 2
    I made you a snippet. I changed the console.log(outputData) Commented Nov 5, 2020 at 7:24

3 Answers 3

2

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; }

Sign up to request clarification or add additional context in comments.

Comments

2

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.

1 Comment

@NinaScholz - I'd forgotten to convert the keys, but did that a minute ago.
1

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.