简体   繁体   English

在Javascript / React中将对象转换为数组

[英]Convert object to array in Javascript / React

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.使用 REST,我正在检索一个对象( JSON格式),该对象将被转换为一个数组,以便可以将其插入到表中。

This is done in the rendering function of React.这是在 React 的渲染函数中完成的。

The input is updated every N minutes from the back-end.输入从后端每 N 分钟更新一次。

How do I convert an object to an array?如何将对象转换为数组?

I need o nly the values, not the keys , since the keys are already present as column values beforehand in the table itself.我只需要值,而不是键,因为键已经预先作为列值存在于表本身中。

You can use Object#values (ECMAScript 2017), but it's not supported by IE (see browser's compatibility ). 您可以使用Object#values (ECMAScript 2017),但IE不支持它(请参阅浏览器的兼容性 )。

Note: The ECMAScript 6 specification defines in which order the properties of an object should be traversed. 注意: ECMAScript 6规范定义了对象的属性应以哪种顺序遍历。 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 : 如果需要支持IE,则可以将Object#keysArray#map

 const map = { a: 1, b: 2, c: 3 }; const result = Object.keys(map).map((key) => map[key]); console.log(result); 

I am not sure by map you mean the Map object or an ordinary JS object. 我不确定map是指Map对象还是普通的JS对象。 However, just for variety I would like to mention that the Map objects are mostly (probably always) stringified like JSON.stringify([...myMap]) . 但是,仅出于多样性,我想提到Map对象大多数(可能总是)像JSON.stringify([...myMap])那样JSON.stringify([...myMap])字符串化。 So if you happen to receive a Map object in JSON data may be you should do something like; 因此,如果您碰巧收到JSON数据中的Map对象,则应该执行类似的操作;

 var myMap = new Map().set(1,"hey").set(2,"you"), mapData = JSON.stringify([...myMap]), values = JSON.parse(mapData).map(d => d[1]); console.log("mapData:",mapData); console.log("values:",values); 

You can set initial value as array firstly.您可以先将初始值设置为数组。 See this example:看这个例子:

const [conversations, setConversations] = useState([]); // fianal data is array

useEffect(() => {
    const fetchConversations = async () => {
       const res = await axios.get("/conversations/" + user._id);              
       setConversations(res.data);              
    };
    fetchConversations();
  }, [user._id]);

res.data convert to array by using useState([]) as initial value and convert to object by using useState({}) . res.data使用useState([])作为初始值转换为数组,并使用useState({})转换为对象。

And map this array:并映射这个数组:

return (
    <>
       {conversations.map((conv) => 
            (<Conversations key={conv._id} conversation={conv} />))}
    </>
)

You can set initial value as array firstly.您可以先将初始值设置为数组。 See this example:看这个例子:

const [conversations, setConversations] = useState([]); // fianal data is array

useEffect(() => {
    const fetchConversations = async () => {
       const res = await axios.get("/conversations/" + user._id);              
       setConversations(res.data);              
    };
    fetchConversations();
  }, [user._id]);

res.data convert to array by using useState([]) as initial value and convert to object by using useState({}) . res.data使用useState([])作为初始值转换为数组,并使用useState({})转换为对象。

And

return( {conversations.map((conv) => ( ))} )返回({conversations.map((conv)=>())})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM