How to convert an array to an object in es6
This article mainly introduces how to convert an array into an object in es6, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.
Conversion methods: 1, the use of fromEntries () and map () function, syntax "Object.fromEntries (item = > [item.key, item])" statement; 2, the use of the extension operator "...", syntax "{... arr}".
The operating environment of this tutorial: windows7 system, ECMAScript version 6, Dell G3 computer.
The method of converting array to object in es6
Method 1: using fromEntries () and map () functions
Const arr = [{key: "id", name: "number"}, {key: "name", name: "name"},]; const obj = Object.fromEntries (item = > [item.key, item]); console.log (obj)
Output
Method 2: use the extension operator "."
Const arr = [{key: "id", name: "number"}, {key: "name", name: "name"},]; const obj = {... arr}; console.log (obj)
Thank you for reading this article carefully. I hope the article "how to convert arrays into objects in es6" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and follow the industry information channel. More related knowledge is waiting for you to learn!