3

I have an arr like this

[{
    "ID": "this THE ID",
    "NAME": "this THE NAME",
    "AGE": "thiS THE AGE"
}, {
    "ID": "that ID",
    "NAME": "that THE NAME",
    "AGE": "that THE AGE"
}]

I'm looking to achieve this result:

[["this THE ID","this THE NAME","thiS THE AGE"], ["that ID","that THE NAME","that THE AGE"]]

What i tried

var result = []
data.forEach(function(ID,NAME,AGE) {
  result.push(ID)
  result.push(NAME)
  result.psuh(AGE)
});

But i am not getting the correct result that i wanted

4 Answers 4

5

Map is your best friend in those kind of situation

 var arr = [{
        "ID": "this THE ID",
        "NAME": "this THE NAME",
        "AGE": "thiS THE AGE"
    }, {
        "ID": "that ID",
        "NAME": "that THE NAME",
        "AGE": "that THE AGE"
    }]
 
 const result = arr.map(({ID,NAME,AGE}) => [ID,NAME,AGE]);
console.log(result)

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

Comments

3

You could map the values, assuming you want the values in insertation order.

var array = [{ ID: "this THE ID", NAME: "this THE NAME", AGE: "thiS THE AGE" }, { ID: "that ID", NAME: "that THE NAME", AGE: "that THE AGE" }],
    result = array.map(Object.values);

console.log(result);

Your function need a destructuring assignment for the properties and you could push an array with the values.

var array = [{ ID: "this THE ID", NAME: "this THE NAME", AGE: "thiS THE AGE" }, { ID: "that ID", NAME: "that THE NAME", AGE: "that THE AGE" }],
    result = [];

array.forEach(function({ ID, NAME, AGE }) {
    result.push([ID, NAME, AGE]);
});

console.log(result);

Comments

2

You almost there:

  • You need to create a nested array for each iteration.
  • You need to destructure the values as follow: function({ID,NAME,AGE}){...}

var data = [{    "ID": "this THE ID",    "NAME": "this THE NAME",    "AGE": "thiS THE AGE"}, {    "ID": "that ID",    "NAME": "that THE NAME",    "AGE": "that THE AGE"}];

var result = []
data.forEach(function({ID,NAME,AGE}) {
  result.push([ID, NAME, AGE] /*Nested array*/);
});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Using the function map:

var data = [{    "ID": "this THE ID",    "NAME": "this THE NAME",    "AGE": "thiS THE AGE"}, {    "ID": "that ID",    "NAME": "that THE NAME",    "AGE": "that THE AGE"}];

var result = data.map(function({ID,NAME,AGE}) {
  return [ID, NAME, AGE];
});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

And the shortest, using arrow function:

var data = [{    "ID": "this THE ID",    "NAME": "this THE NAME",    "AGE": "thiS THE AGE"}, {    "ID": "that ID",    "NAME": "that THE NAME",    "AGE": "that THE AGE"}];

var result = data.map(({ID,NAME,AGE}) => ([ID, NAME, AGE]));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

1 Comment

Thank you , your solution look close to mine
1

You may do as follows;

var arr = [{ "ID": "this THE ID",
             "NAME": "this THE NAME",
             "AGE": "thiS THE AGE"
           },
           { "ID": "that ID",
             "NAME": "that THE NAME",
             "AGE": "that THE AGE"
           }],
    brr = arr.map(Object.values);
 console.log(brr);

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.