Answer a question

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.

Answers

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);
Logo

React社区为您提供最前沿的新闻资讯和知识内容

更多推荐