Home > Web Front-end > JS Tutorial > How to convert array to object in es6

How to convert array to object in es6

青灯夜游
Release: 2023-01-29 14:20:04
Original
8419 people have browsed it

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);
Copy after login

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);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template