Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shahriyar-hosen/array-of-object-single-object
https://github.com/shahriyar-hosen/array-of-object-single-object
Last synced: 4 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/shahriyar-hosen/array-of-object-single-object
- Owner: Shahriyar-Hosen
- Created: 2022-10-14T17:43:29.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2024-05-14T03:44:28.000Z (6 months ago)
- Last Synced: 2024-06-07T18:02:59.162Z (5 months ago)
- Language: TypeScript
- Size: 2.93 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Merge multiple objects inside the same array into one object [duplicate]
```sh
/*
let arrObj = [
{ a: 1, b: 2 },
{ c: 3, d: 4 },
{ e: 5, f: 6 },
];how can you merge this into one obj?
*/
//mergedObj = {a:1, b:2, c:3, d:4, e:5, f:6}
//Answers
const arrObj = [
{ a: 1, b: 2 },
{ c: 3, d: 4 },
{ e: 5, f: 6 },
];// 1.
console.log(
arrObj.reduce(function (result, current) {
return Object.assign(result, current);
}, {})
);// 2.
// If you prefer arrow functions, you can make it a one-liner ;-)
console.log(arrObj.reduce((r, c) => Object.assign(r, c), {}));// 3.
// Thanks Spen from the comments. You can use the spread operator with assign
console.log(Object.assign({}, ...arrObj));
```