Nest all content into snippets

This commit is contained in:
Angelos Chalaris
2023-05-07 16:07:29 +03:00
parent 2ecadbada9
commit 6a45d2ec07
1240 changed files with 0 additions and 0 deletions

29
snippets/js/s/to-pairs.md Normal file
View File

@ -0,0 +1,29 @@
---
title: Object to pairs
type: snippet
language: javascript
tags: [object,array]
author: chalarangelo
cover: purple-leaves
dateModified: 2020-10-22T20:24:30+03:00
---
Creates an array of key-value pair arrays from an object or other iterable.
- Check if `Symbol.iterator` is defined and, if so, use `Array.prototype.entries()` to get an iterator for the given iterable.
- Use `Array.from()` to convert the result to an array of key-value pair arrays.
- If `Symbol.iterator` is not defined for `obj`, use `Object.entries()` instead.
```js
const toPairs = obj =>
obj[Symbol.iterator] instanceof Function && obj.entries instanceof Function
? Array.from(obj.entries())
: Object.entries(obj);
```
```js
toPairs({ a: 1, b: 2 }); // [['a', 1], ['b', 2]]
toPairs([2, 4, 8]); // [[0, 2], [1, 4], [2, 8]]
toPairs('shy'); // [['0', 's'], ['1', 'h'], ['2', 'y']]
toPairs(new Set(['a', 'b', 'c', 'a'])); // [['a', 'a'], ['b', 'b'], ['c', 'c']]
```