784 B
784 B
title, tags, expertise, author, cover, firstSeen
| title | tags | expertise | author | cover | firstSeen |
|---|---|---|---|---|---|
| Unwind object | object | intermediate | chalarangelo | blog_images/waves-from-above.jpg | 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
keyfrom the object. - Use
Array.prototype.map()for the values of the givenkeyto create an array of objects. - Each object contains the original object's values, except for
keywhich is mapped to its individual values.
const unwind = (key, obj) => {
const { [key]: _, ...rest } = obj;
return obj[key].map(val => ({ ...rest, [key]: val }));
};
unwind('b', { a: true, b: [1, 2] }); // [{ a: true, b: 1 }, { a: true, b: 2 }]