# @egjs/list-differ 中文文档

规范地址：https://npmcn.com/package/%40egjs/list-differ/

版本：1\.0\.1
npm 元数据更新时间：2025-10-24T06:49:56.874Z
中文译文更新时间：2026-11-13T09:05:57.865Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40egjs/list-differ
原文对照：https://npmcn.com/package/%40egjs/list-differ/?lang=original
项目主页：https://github.com/naver/egjs-list-differ#readme
源码仓库：https://github.com/naver/egjs-list-differ

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：104 kB，19 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- 首次发布：2019-05-08T11:56:39.940Z
- 最新版本发布：2023-04-07T04:10:42.717Z
- 近 30 天下载量：3477435（2026-10-13 至 2026-11-11，npm 官方下载统计）
- 已发布版本数：6（近 12 个月 0 个）

## 中文简介

在数组中添加、删除或更改值时检查 diff 的模块.

## 安装

```sh
npm install @egjs/list-differ
```

也可以使用：`pnpm add @egjs/list-differ`、`yarn add @egjs/list-differ`、`bun add @egjs/list-differ`

## README

![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/demo/images/logo.png)

## @ egjs/ list- divers

[![version](https://img.shields.io/npm/v/@egjs/list-differ.svg?style=flat-square&color=007acc&label=version&logo=NPM)](https://www.npmjs.com/package/@egjs/list-differ) [![Travis (.org)](https://img.shields.io/travis/naver/egjs-list-differ.svg?style=flat-square&label=build&logo=travis%20ci)](https://travis-ci.org/naver/egjs-list-differ) [![Coveralls github](https://img.shields.io/coveralls/github/naver/egjs-list-differ.svg?style=flat-square&label=%E2%9C%85%20coverage)](https://coveralls.io/github/naver/egjs-list-differ?branch=master&style=flat-square) [![npm downloads per month](https://img.shields.io/npm/dm/@egjs/list-differ.svg?style=flat-square&label=%E2%AC%87%20downloads&color=08CE5D)](https://www.npmjs.com/package/@egjs/list-differ) [![GitHub](https://img.shields.io/github/license/naver/egjs-list-differ.svg?style=flat-square&label=%F0%9F%93%9C%20license&color=08CE5D)](https://github.com/naver/egjs-list-differ/blob/master/LICENSE)

QQ 模块, 在一个阵列中添加、 删除或更改值时检查 diff .

## \* 安装

```sh
$ npm i @egjs/list-differ
```

```html
<script src="//naver.github.io/egjs-list-differ/release/latest/dist/list-differ.min.js"></script>
```

## 页:1

-   见 [**文档**](https://naver.github.io/egjs-list-differ/release/latest/doc/index.html) 页 次.
-   [引入列表差异以跟踪数据和跟踪变化](https://medium.com/naver-fe-platform/introducing-listdiffer-to-track-changes-in-data-27793f0c6f4a)(单位:千美元)[한국어](https://medium.com/naver-fe-platform/%EB%8D%B0%EC%9D%B4%ED%84%B0%EC%9D%98-%EB%B3%80%ED%99%94%EB%A5%BC-%EC%95%8C%EC%95%84%EB%82%B4%EA%B3%A0-%EB%B3%80%ED%99%94-%EA%B3%BC%EC%A0%95%EC%9D%84-%EC%B6%94%EC%A0%81-%ED%95%98%EB%8A%94-listdiffer-9c3f1d770542)(中文(简体) )
-   [如何建立跨框架构成部分](https://medium.com/naver-fe-platform/how-to-make-cross-framework-component-ee76d76708b1)(单位:千美元)[한국어](https://medium.com/naver-fe-platform/cross-framework-component%EB%A5%BC-%EB%A7%8C%EB%93%9C%EB%8A%94-%EB%B0%A9%EB%B2%95-234b3fece353)(中文(简体) )

## • 软件包

| 软件包 | 版本 | 说明 |
| --- | --- | --- |
| [**@egjs/ 儿童差异**](https://github.com/naver/egjs-children-differ) | [![version](https://img.shields.io/npm/v/@egjs/children-differ.svg?style=flat-square&color=007acc&label=%F0%9F%94%96)](https://www.npmjs.com/package/@egjs/children-differ) | 一个测试DOM孩子的模块. |
| [**@egjs/ react- children-differ (英语)**](https://github.com/naver/egjs-children-differ/blob/master/packages/react-children-differ/README.md) | [![version](https://img.shields.io/npm/v/@egjs/react-children-differ.svg?style=flat-square&color=00d8ff&label=%F0%9F%94%96)](https://www.npmjs.com/package/@egjs/react-children-differ) | ![React](https://naver.github.io/egjs-flicking/images/react.svg) [反应](https://reactjs.org/) @egjs/ children-differ 的端口 |
| [**@egjs/ngx- 儿童- differ**](https://github.com/naver/egjs-children-differ/blob/master/packages/ngx-children-differ/README.md) | [![version](https://img.shields.io/npm/v/@egjs/ngx-children-differ.svg?style=flat-square&color=dd0031&label=%F0%9F%94%96)](https://www.npmjs.com/package/@egjs/ngx-children-differ) | ![Angular](https://naver.github.io/egjs-flicking/images/angular.svg) [角质](https://angular.io/) @egjs/ children-differ 的端口 |
| [**@egjs/vue- children-differ (英语)**](https://github.com/naver/egjs-children-differ/blob/master/packages/vue-children-differ/README.md) | [![version](https://img.shields.io/npm/v/@egjs/vue-children-differ.svg?style=flat-square&color=42b883&label=%F0%9F%94%96)](https://www.npmjs.com/package/@egjs/vue-children-differ) | ![Vue.js](https://naver.github.io/egjs-flicking/images/vue.svg) [Vue.js (英语)](https://vuejs.org/v2/guide/index.html) @egjs/ children-differ 的端口 |

## 如何使用

### 检查数组中的 diff

```js
import ListDiffer, { diff } from "@egjs/list-differ";

// script => eg.ListDiffer
// Value is key
const differ = new ListDiffer([1, 2, 3, 4, 5, 6, 7], e => e);
// const result = diff([1, 2, 3, 4, 5, 6, 7], [4, 3, 6, 2, 1, 7], e => e);
const result = differ.update([4, 3, 6, 2, 1, 7]);
// List before update
// [1, 2, 3, 4, 5, 6, 7]
console.log(result.prevList);
// Updated list
// [4, 3, 6, 2, 1, 7]
console.log(result.list);
// Index array of values added to `list`
// [2]
console.log(result.added);
// Index array of values removed in `prevList`
// [5, 4]
console.log(result.removed);
// An array of index pairs of `prevList` and `list` with different indexes from `prevList` and `list`
// [[3, 0], [2, 1], [1, 3], [0, 4], [6, 5]]
console.log(result.changed);
// The subset of `changed` and an array of index pairs that moved data directly. Indicate an array of absolute index pairs of `ordered`.(Formatted by: Array<[index of prevList, index of list]>)
// [[3, 0], [2, 1], [1, 3]]
console.log(result.pureChanged);
// An array of index pairs to be `ordered` that can synchronize `list` before adding data. (Formatted by: Array<[prevIndex, nextIndex]>)
// [[3, 0], [3, 1], [3, 2]]
console.log(result.ordered);
// An array of index pairs of `prevList` and `list` that have not been added/removed so data is preserved
// [[3, 0], [2, 1], [1, 3], [0, 4], [6, 5]]
console.log(result.maintained);
```

### 改变了什么?

-   **已更改**: 一系列指数对 `prevList` 财务报告和审定财务报表 `list` 不同指数 `prevList` 财务报告和审定财务报表 `list`
-   **纯变形**: 子集 `changed` 和一系列直接移动数据的索引对。 表示绝对指数对的数组 `ordered`.(由: Array < \[prevList的索引,列表索引\] 校正)

|  | **已更改** | **纯已改变 :** |
| --- | --- | --- |
|  | \[\[3、0、\[2、1\]、\[1、3\]、\[0、4\]、\[6、5\]\] | \[\[ \[3, \], \[2, 1\], \[1, 3\]\]\]\] |
| 预览列表 | ![prev\_list](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/changed_prev_list.png) | ![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/changed_before_prev_list.png) |
| 进程 | 
\- 说吧

 | ![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/changed_before_animation.gif) |
| 列表 | ![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/changed_list.png) | ![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/changed_before_list.png) |

### 什么订单?

一系列索引配对 `ordered` 能够同步 `list` 在添加数据之前。 (原载:Array<\[prevIndex,下Index\])

|  | 删除 - > 命令 - > 添加 |
| --- | --- |
| 预览列表 | ![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/prev_list.png) |
| 删除  
\[5、4\] | ![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/removed.png) |
| 订购 | \[\[3, 0, \[3, 1\], \[3, 2\]\]\]\] |
|  | ![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/ordered_before_added.gif) |
| 命令 \[0\]  
\[3=>0\] | ![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/ordered_before_ordered0.png) |
| 命令 \[1\]  
\[3=> 1\] | ![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/ordered_before_ordered1.png) |
| 命令 \[2\]  
\[3=>2\] | ![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/ordered_before_ordered2.png) |
| 已添加  
\[2\] | ![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/ordered_before_added.png) |
| 列表 | ![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/list.png) |

## 数据同步示例

```js
import ListDiffer, { diff } from "@egjs/list-differ";

const prevList = [1, 2, 3, 4, 5, 6, 7];
const list = [4, 3, 6, 2, 1, 7];
// const differ = new ListDiffer(prevList, e => e);
// const result = differ.update(list);
const result = diff(prevList, list, e => e);
```

### 删除qq命令 添加

|  | 删除qq命令 添加 |
| --- | --- |
| 预览列表 | ![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/prev_list.png) |
| 进程 | ![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/changed_before_animation.gif) |
| 列表 | ![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/list.png) |

-   同步列表

```js
const nextList = prevList.slice();
result.removed.forEach(index => {
  nextList.splice(index, 1);
});
result.ordered.forEach(([from, to], i) => {
  nextList.splice(from, 1);
  nextList.splice(to, 0, list[result.pureChanged[i][1]]);
});
result.added.forEach(index => {
  nextList.splice(index, 0, list[index]);
});
// `nextList` is the same as `list`.
console.log(nextList);
```

-   同步 DOM

```js
const parentElement = document.querySelector(".parent");
const children = parentElement.children;

result.removed.forEach(index => {
  children[index].remove();
});
result.ordered.forEach(([from, to]) => {
  parentElement.insertBefore(children[from], children[from < to ? to + 1 : to]);
});
result.added.forEach(index => {
  parentElement.insertBefore(document.createElement("div"), children[index]);
});
```

### 删除 qq 维护 qq 添加

|  | 已保留 +++ 添加 |
| --- | --- |
| 预览列表 | ![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/prev_list.png) |
| 进程 | ![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/maintained_with_added.gif) |
| 列表 | ![](https://raw.githubusercontent.com/naver/egjs-list-differ/master/images/list.png) |

-   同步列表

```js
const nextList: number[] = [];
result.removed.forEach(index => {
  // There is a remove operation for the index.
  // prevList[index];
});
result.maintained.forEach(([from, to]) => {
  nextList[to] = list[to];
});
result.added.forEach(index => {
  nextList[index] = list[index];
});
// `nextList` is the same as `list`.
console.log(nextList);
```

-   同步 DOM

```js
const parentElement = document.querySelector(".parent");
const children = parentElement.children;
const prevChildren: Element[] = [].slice.call(parentElement.children);

result.removed.forEach(index => {
  prevChildren[index].remove();
});
result.maintained.forEach(([from, to]) => {
  parentElement.appendChild(prevChildren[from]);
});
result.added.forEach(index => {
  parentElement.insertBefore(document.createElement("div"), children[index]);
});
```

## 捐款

见 [捐款额](https://github.com/naver/egjs-list-differ/blob/master/CONTRIBUTING.md)。 。 。 .

## \* 反馈

请存档 [问题](https://github.com/naver/egjs-list-differ/issues)。 。 。 .

## 许可证

@egjs/list-differ 在下方发布 [麻省理工学院许可证](https://github.com/naver/egjs-list-differ/blob/master/LICENSE)。 。 。 .

```
Copyright (c) 2019-present NAVER Corp.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
```

## 常见问题

### @egjs/list-differ 是什么？

在数组中添加、删除或更改值时检查 diff 的模块\.

### 如何安装 @egjs/list-differ？

在项目目录运行 npm install @egjs/list-differ，或使用 pnpm add / yarn add / bun add。

### @egjs/list-differ 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @egjs/list-differ 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @egjs/list-differ 可以商用吗？

@egjs/list-differ 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @egjs/list-differ 的下载量有多少？

根据 npm 官方下载统计，@egjs/list-differ 在 2026/10/13 – 2026/11/11 这 30 天内被下载 3,477,435 次，日均约 115,915 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @egjs/list-differ 还在维护吗？

最新版本 1\.0\.1 发布于 2023/04/07（3 年前）；至今共发布 6 个版本，其中过去 12 个月发布了 0 个；首次发布于 2019/05/08。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

