How to convert key-value pair object into an array of values in ES6?

Viewed 17425

I'm developing a React application where I need to convert a key-value object like this:

{
  0: 'John',
  1: 'Tim',
  2: 'Matt'
};

To an array of just the values like this:

['John', 'Tim', 'Matt']

How do I accomplish this?

const obj = {
  0: 'John',
  1: 'Tim',
  2: 'Matt'
};

const arr = /* ??? */;
5 Answers

This is commonly used one:

const obj={
           1:'Azamat',
           2: 'Kanybek',
           3: 'Manas'}

console.log(Object.values(obj))

for key, value pairs:

const arr = [];

for(let key in obj){
  arr.push([key,obj[key]])
  
}
console.log(arr)
Related