Skip to content Skip to sidebar Skip to footer

How I Can Can Map An Large Object To An Array In Es6

Suppose I have large object var object = { a: 1, b: 2, c: 3, d:4, e:5, f:6, g:7, h:8, i:9, j:10 ...}; var array = []; Output is [1,2,3,4,5,6,7,8,9,10...] how I can map object to

Solution 1:

You could use the keys, order them and return the value in an array.

var object = { a: 1, b: 2, c: 3, d:4, e:5, f:6, g:7, h:8, i:9, j:10};
var array = Object.keys(object).sort().map(k => object[k]);

console.log(array);

Solution 2:

You can use Object.keys()

let arr = Object.keys(obj).map((k) => obj[k])

Solution 3:

Depending on which browsers you're wanting to support, you can achieve this using Object.values() which is part of ECMAScript 2017:

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...

MDN's Documentation on Object.values()

var
  obj = { a: 1, b: 2, c: 3, d:4, e:5, f:6, g:7, h:8, i:9, j:10 },
  values = Object.values(obj)
;

console.log(values);

Solution 4:

Try this simple approach

var object = { a: 1, b: 2, c: 3, d:4, e:5, f:6, g:7, h:8, i:9, j:10};
var array = Object.keys( object ).map( function(key){ return object[key] } );
console.log(array);

Post a Comment for "How I Can Can Map An Large Object To An Array In Es6"