Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
184
rated 0 times [  187] [ 3]  / answers: 1 / hits: 83969  / 8 Years ago, thu, january 5, 2017, 12:00:00

Using REST, I am retrieving an object(JSON format) which is to be converted to an array so that it can be inserted into a table.



This is done in the rendering function of React.



The input is updated every N minutes from the back-end.



How do I convert an object to an array?



I need only the values, not the keys, since the keys are already present as column values beforehand in the table itself.


More From » arrays

 Answers
372

You can use Object#values (ECMAScript 2017), but it's not supported by IE (see browser's compatibility).



Note: The ECMAScript 6 specification defines in which order the properties of an object should be traversed. This blog post explains the details.





const map = { a: 1, b: 2, c: 3 };

const result = Object.values(map);

console.log(result);





If you need to support IE, you can use Object#keys with Array#map:





const map = { a: 1, b: 2, c: 3 };

const result = Object.keys(map).map((key) => map[key]);

console.log(result);




[#59453] Tuesday, January 3, 2017, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tyasiaalmap

Total Points: 294
Total Questions: 107
Total Answers: 108

Location: Libya
Member since Mon, Dec 7, 2020
4 Years ago
;