4

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 = /* ??? */;
2

5 Answers 5

12

You could use Object.values.

The Object.values() method returns an array of a given object's own enumerable property values, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).

var object = { 0: 'John', 1: 'Tim', 2: 'Matt' }, 
    array = Object.values(object);
    
console.log(array);

With ES6, you could use Array.from and use a callback for the values.

var object = { 0: 'John', 1: 'Tim', 2: 'Matt' }, 
    array = Array.from(Object.keys(object), k => object[k]);
    
console.log(array);

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

1 Comment

Object.values is not in ES6 included, coming with ES7. He wants something in ES6.
7

You can make use of Object.values command.

According to the docs.

Object.values() returns an array whose elements are the enumerable property values found on the object. The ordering of the properties is the same as that given by looping over the property values of the object manually

Although it is an ES2017 solution but since you are using react, you can include stage-0 as a preset for babel and access this functionality

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

var newdata = Object.values(data);
console.log(newdata);

there are other methods like Object.keys which gives you all the keys as an array and Object.entries method returns an array of a given object's own enumerable property [key, value] pairs which might also be useful to you

3 Comments

Great solution, but it's an ES2017 feature and not supported by older browsers.
@D.Simon you are right, however since OP is using react, he can make use of stage-0 preset to use it
@D.Simon So is const. As for Object methods, there are polyfills!
4
const obj = {
  0: 'John',
  1: 'Tim',
  2: 'Matt'
};

const arr = [];
for(let key in obj){
  arr.push(obj[key]);
}

Comments

0

While you have numerical keys and in an order without gaps, you could use Object.assign with an array as target and the given object as source.

var object = { 0: 'John', 1: 'Tim', 2: 'Matt' }, 
    array = Object.assign([], object);
    
console.log(array);

Comments

0

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)

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.