0

I have array of objects like this

let array = [
 {'name' : 'Jack', 'age':23},
 {'name': 'Robin', 'age':25}
];

and I want an array like this

[
 ['Jack',23],
 ['Robin',25]
]

I tried this code

var myArr = []; 

var input = [
  {name : 'Jack', age : 23},
  {name : 'Robin', age : 25}
];

input.forEach((item,index)=>{
  for (var k in item) { 
    myArr.push(item[k]);
  }
})

But it is producing result like this

["Jack", 23, "Robin", 25]

3 Answers 3

4

let array = [
 {'name' : 'Jack', 'age':23},
 {'name': 'Robin', 'age':25}
];

arrayOfValues = array.map(({name, age})=> ([name, age]))

console.log(arrayOfValues)

@HMR Thank you very much for sharing the valuable information.

The order in which you get the values in Object.values or Object.keys or for ... in or for ... of from an object (other than a Map) is not guaranteed and there is no specification in JS for the order.

More Information

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

3 Comments

The order in which you get the values in Object.values or Object.keys or for ... in or for ... of from an object (other than a Map) is not guaranteed and there is no specification in JS for the order. So if you want to convert {'name' : 'Jack', 'age':23} specifically to ['Jack',23] then this is the only valid answer.
You're Correct @HMR
Mohammed pls consider adding @HMR's point as your explanation so that others are also educated with the same and are not mislead by the accepted answer.
3

Simply pass Object.values as callback to .map():

let array = [
 {'name' : 'Jack', 'age':23},
 {'name': 'Robin', 'age':25}
];

let result = array.map(Object.values);

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

1 Comment

The order in which you get the values in Object.values or Object.keys or for ... in or for ... of from an object (other than a Map) is not guaranteed and there is no specification in JS for the order. So if you want to convert {'name' : 'Jack', 'age':23} specifically to ['Jack',23] then this answer will work by accident but not because it's guaranteed by JS specification. You should never write code relying on the current order of getting keys from objects because it can be changed and will break if that happens.
2

This would be Array.map and Object.values like below

let array = [
 {'name' : 'Jack', 'age':23},
 {'name': 'Robin', 'age':25}
];

let res = array.map(d => Object.values(d))

console.log(res)

1 Comment

The order in which you get the values in Object.values or Object.keys or for ... in or for ... of from an object (other than a Map, Set or Array) is not guaranteed and there is no specification in JS for the order. So if you want to convert {'name' : 'Jack', 'age':23} specifically to ['Jack',23] then this answer will work by accident but not because it's guaranteed by JS specification. You should never write code relying on the current order of getting keys from objects because it can be changed and will break if that happens.

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.