Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

javascript - How to convert array of objects into array of arrays

The array of objects looks like this:

[
    {"Number":64,"Earning":160000},{"Number":64,"Earning":160000},
    {"Number":64,"Earning":160000},{"Number":64,"Earning":160000},
    {"Number":64,"Earning":160000},{"Number":64,"Earning":160000},
    {"Number":64,"Earning":160000},{"Number":64,"Earning":160000}
]

and the required JavaScript array is:

[
    [160000, 64], [160000, 64], [160000, 64], [160000, 64],
    [160000, 64], [160000, 64], [160000, 64], [160000, 64]
]

I want to convert JSON object data into JavaScipt and JSON data which I supply is given above and JavaScript array which I want is also defined. I want a JavaScript methode which solves this problem

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you're using this exact structure, you can do something like this. Where inputData is the first JSON structure, and outputData will be the data outputted.

var outputData = [];

for(var i = 0; i < inputData.length; i++) {
    var input = inputData[i];

    outputData.push([input.Earning, input.Number]);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...