


How to Convert an Array of Objects to a Single Key-Value Pair Object in JavaScript?
Nov 08, 2024 am 09:38 AMConverting an Array of Objects to an Object with Key-Value Pairs
Question:
How can you transform an array of objects into a single object with key-value pairs in JavaScript?
Support Requirement:
The solution should be compatible with a wide range of browsers.
Answer:
To effectively merge an array of objects into a single object, utilize Object.assign() in conjunction with spread syntax (...). This technique allows you to create a new object with the merged properties from the original array. Consider the following code snippet:
var array = [{ name1: "value1" }, { name2: "value2" }] var object = Object.assign({}, ...array) console.log(object)
This code creates an object object by spread-expanding the array array and merging its properties into an empty object {} using Object.assign(). The console.log() statement prints the resulting object object, which contains key-value pairs extracted from the original array.
The above is the detailed content of How to Convert an Array of Objects to a Single Key-Value Pair Object in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Replace String Characters in JavaScript

HTTP Debugging with Node and http-console

Custom Google Search API Setup Tutorial
