Prepare repository for merge

This commit is contained in:
Angelos Chalaris
2023-05-01 22:35:56 +03:00
parent fc4e61e6fa
commit b3ad01863a
578 changed files with 0 additions and 0 deletions

View File

@ -0,0 +1,25 @@
---
title: Unwind object
type: snippet
tags: [object]
author: chalarangelo
cover: waves-from-above
dateModified: 2022-04-18T05:00:00-04:00
---
Produces an array of objects from an object and one of its array-valued properties.
- Use object destructuring to exclude the key-value pair for the specified `key` from the object.
- Use `Array.prototype.map()` for the values of the given `key` to create an array of objects.
- Each object contains the original object's values, except for `key` which is mapped to its individual values.
```js
const unwind = (key, obj) => {
const { [key]: _, ...rest } = obj;
return obj[key].map(val => ({ ...rest, [key]: val }));
};
```
```js
unwind('b', { a: true, b: [1, 2] }); // [{ a: true, b: 1 }, { a: true, b: 2 }]
```