Created
August 10, 2024 16:41
-
-
Save undfine/8b93430083673a0670f07fc7f64133f5 to your computer and use it in GitHub Desktop.
Convert array of objects to a single object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function arrayToObject(arr) { | |
| return arr.reduce((acc, curr) => { | |
| acc[curr.name] = curr.value; | |
| return acc; | |
| }, {}); | |
| } | |
| // Example usage: | |
| const inputArray = [ | |
| { name: "firstName", value: "John" }, | |
| { name: "lastName", value: "Doe" }, | |
| { name: "age", value: 30 }, | |
| ]; | |
| const result = arrayToObject(inputArray); | |
| console.log(result); | |
| // Output: { firstName: 'John', lastName: 'Doe', age: 30 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment