diff --git a/README.md b/README.md index b32ab8e08..b13302e49 100644 --- a/README.md +++ b/README.md @@ -7084,7 +7084,7 @@ Creates an object from the given key-value pairs. Use `Array.reduce()` to create and combine key-value pairs. ```js -const objectFromPairs = arr => arr.reduce((a, v) => ((a[v[0]] = v[1]), a), {}); +const objectFromPairs = arr => arr.reduce((a, [key, val]) => ((a[key] = val), a), {}); ```
diff --git a/docs/object.html b/docs/object.html index f3d2097ba..addef240f 100644 --- a/docs/object.html +++ b/docs/object.html @@ -278,7 +278,7 @@ Foo.prototype: 5, parent_id: 4 } ]; const nestedComments = nest(comments); // [{ id: 1, parent_id: null, children: [...] }] -

objectFromPairs

Creates an object from the given key-value pairs.

Use Array.reduce() to create and combine key-value pairs.

const objectFromPairs = arr => arr.reduce((a, v) => ((a[v[0]] = v[1]), a), {});
+

objectFromPairs

Creates an object from the given key-value pairs.

Use Array.reduce() to create and combine key-value pairs.

const objectFromPairs = arr => arr.reduce((a, [key, val]) => ((a[key] = val), a), {});
 
objectFromPairs([['a', 1], ['b', 2]]); // {a: 1, b: 2}
 

objectToPairs

Creates an array of key-value pair arrays from an object.

Use Object.keys() and Array.map() to iterate over the object's keys and produce an array with key-value pairs.

const objectToPairs = obj => Object.keys(obj).map(k => [k, obj[k]]);
 
objectToPairs({ a: 1, b: 2 }); // [['a',1],['b',2]]