-3

I'm trying to convert an array of object which looks like

var someJsonObj = [
  {id: 0, name: "name", property: "value", otherproperties: "othervalues"},
  {id: 1, name: "name1", property: "value1", otherproperties: "othervalues1"},
  {id: 2, name: "name2", property: "value2", otherproperties: "othervalues2"}
];

to

var someArray = [
  [0,"name","value","othervalues"],
  [1,"name1","value1","othervalues1"],
  [2,"name2","value2","othervalues2"]
]

using .push but what I'm getting are three empty arrays or one array of first values like [0,1,2]

        var someArray=[];
        for(var i=0;i<someJsonObj.length;i++){
            someArray.push(someJsonObj[i].val);
        }
4
  • 1
    What code have you tried? Commented Jun 11, 2018 at 20:13
  • 4
    Possible duplicate of Convert array of objects to array of arrays Commented Jun 11, 2018 at 20:22
  • @Xufox hard to find that one as no one voted for that question :| Commented Jun 11, 2018 at 20:27
  • @JackTheKnife It was at +1/-1. Commented Jun 11, 2018 at 20:28

1 Answer 1

4

Use Object.values() in es6:

  const someArray = someJsonObj.map( obj => Object.values(obj) );
Sign up to request clarification or add additional context in comments.

4 Comments

If you want to guarantee order, use .map(({id, name, property, otherproperties}) => [id, name, property, otherproperties]) instead.
Also, if you don't follow @Xufox advise, I would provide Object.values as is: someJsonObj.map(Object.values) :P
Does that actually work? I tend to worry about problems with binding this when using Object.whatever()
Somehow I'm getting a string instead of array of values. More here: stackoverflow.com/questions/50805989/…

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.