-1

When I do JArray.toSource(), I get this:-

[{selectionID:"1", cnt:"5"}, {selectionID:"2", cnt:"2"}, {selectionID:"3", cnt:"1"}]

How can I convert JArray to format like this:-

[[1,5],[2,2],[3,1]]

in JavaScript.

Eventually I want newJArray.toSource() become [[1,5],[2,2],[3,1]]

2 Answers 2

3

You can use Object.keys which returns the keys, and map to map the array as such:

array.map(function(arrayItem) {
  return Object.keys(arrayItem).map(function(objectKey) {
    return +arrayItem[objectKey]; // convert to number
  });
});

Basically, you replace each element in the array with the values of the object.

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

Comments

1

pimvdb's solution is very nice. My comment is that in this particular case this is enough:

array.map(function(arrayItem) {
    return [arrayItem.selectionID, arrayItem.cnt];
});

It is uglier (more specific), but my gut feeling is that it's much more efficient (and sometimes that matters).

1 Comment

newJArray= JArray.map(function(){ return [JArray.selectionID, JArray.cnt]; }); become [[(void 0), (void 0)], [(void 0), (void 0)], [(void 0), (void 0)]] Am I missing something here where JArray[??].selectionID

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.