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 an array in es6 ? please help me.
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 an array in es6 ? please help me.
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);
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 afor...in
loop...
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);
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);