[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

How to convert array to object in es6

青灯夜游
青灯夜游Original
2022-03-08 16:38:208364browse

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}".

How to convert array to object in es6

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

How to convert array to object in es6

Method 2: Use the spread operator "..."

const arr = [
  { key: "id", name: "编号" },
  { key: "name", name: "名称" },
];

const obj = {...arr} ;

console.log(obj);

How to convert array to object in es6

[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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn