Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hjxenjoy/array2map
https://github.com/hjxenjoy/array2map
Last synced: 10 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/hjxenjoy/array2map
- Owner: hjxenjoy
- Created: 2021-03-16T07:10:00.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2021-03-16T10:12:01.000Z (over 3 years ago)
- Last Synced: 2024-10-11T15:38:55.000Z (about 1 month ago)
- Language: JavaScript
- Size: 15.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# arr2map
transform array to map / array
```sh
# use npm
npm install --save arr2map
# use yarn
yarn add arr2map
``````js
import { array2item, array2bool, array2children } from 'arr2map'const arr = [{ id: 1, name: 'hjx' }, { id: 2, name: 'enjoy' }]
const arr2 = ['a', 'b']
const arr3 = [2, 5]array2bool(arr, 'id')
// { 1: true, 2: true }array2bool(arr2)
// { a: true, b: true }array2bool(arr3)
// { 2: true, 5: true }array2item(arr, 'id')
// { 1: { id: 1, name: 'hjx'}, 2: { id: 2, name: 'enjoy' } }array2item(arr, item => item.id, item => item.name)
// { 1: 'hjx', 2: 'enjoy' }array2item(arr3, item => `double-${item}`, item => item * 2)
// { double-2: 4, double-5: 10 }const arr4 = [{
id: 1,
name: 'hjx',
codes: [{ id: 3, name: 'h' }, { id: 4, name: 'jx' }]
}, {
id: 2,
name: 'enjoy',
codes: [{ id: 5, name: 'en' }, { id: 6, name: 'joy' }]
}]array2children(arr4, item => item.codes)
// [{ id: 3, name: 'h' }, { id: 4, name: 'jx' }, { id: 5, name: 'en' }, { id: 4, name: 'joy' }]
```