# list2map

> 将list转换为map

Latest version **1.0.0** (published 2022-05-08) · ISC license · 0 weekly downloads

## Install

```sh
npm install list2map
pnpm add list2map
yarn add list2map
bun add list2map
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2022-05-08 |
| First published | 2022-02-14 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 3.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | zxyuling |
| Maintainers | zxyuling |
| Keywords | list2map |

## Links

- npm: https://www.npmjs.com/package/list2map
- Repository: https://github.com/zxyuling/list2map
- Homepage: https://github.com/zxyuling/list2map#readme
- Issues: https://github.com/zxyuling/list2map/issues
- npm.io page: https://npm.io/package/list2map

## Recent versions

- 1.0.0 (latest) — 2022-05-08
- 0.0.1 — 2022-02-14

## README

# list2map

将 list 转换为 map

### 普通数组

如果不传入第二个参数或传入`null`，将会以数组的`index`作为`map`的`key`

```js
const list = [0, 1, 2];
const map = list2map(list);
//map:{0: 0, 1: 1, 2: 2};
```

### 对象数组

值得注意的是`map`的`value`将会和对象数组的`项`具有相同引用

```js
const list = [
  { id: "id_0", content: "a" },
  { id: "id_1", content: "b" },
  { id: "id_2", content: "c" },
];
const map = list2map(list, "id");
//map:
//  {
//   id_0: { id: "id_0", content: "a" },
//   id_1: { id: "id_1", content: "b" },
//   id_2: { id: "id_2", content: "c" },
// };


```

### 值格式化
第三个参数为格式化函数，入参为数组的`项` ，出参将会作为`map`的`值`

```js
const list = [
  { id: "id_0", content: "a" },
  { id: "id_1", content: "b" },
  { id: "id_2", content: "c" },
];
const map = list2map(list, "id", (value) => value.content);
//map:{"id_0": "a", "id_1": "b", "id_2":"c"};

```

---
_Source: https://npm.io/package/list2map · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
