[item.key, item]))" statement; 2. Use the expansion operator "... ", syntax "{...arr}"."/> [item.key, item]))" statement; 2. Use the expansion operator "... ", syntax "{...arr}".">
Home > Article > Web Front-end > How to convert array to object in es6
Conversion method: 1. Use the fromEntries() and map() functions, the syntax "Object.fromEntries(arr.map(item => [item.key, item]))" statement; 2. Use Spread operator "...", syntax "{...arr}".
The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
Method to convert array to object in es6
Method 1: Using fromEntries() and map() functions
const arr = [ { key: "id", name: "编号" }, { key: "name", name: "名称" }, ]; const obj = Object.fromEntries(arr.map(item => [item.key, item])); console.log(obj);
Output
Method 2: Use the spread operator "..."
const arr = [ { key: "id", name: "编号" }, { key: "name", name: "名称" }, ]; const obj = {...arr} ; console.log(obj);
[Related recommendations: javascript video tutorial, web front-end】
The above is the detailed content of How to convert array to object in es6. For more information, please follow other related articles on the PHP Chinese website!