✨✨ 对比两个列表的差异,计算最小编辑距离
$ npm i --save @monajs/list-diff
import listDiff from '@monajs/list-diff'
const oldList = [
{ key: '1', name: 'a' },
{ key: '2', name: 'b' },
{ key: '3', name: 'c' },
{ key: '4', name: 'd' }
]
const newList = [
{ key: '2', name: 'b' },
{ key: '5', name: 'e' },
{ key: '6', name: 'f' },
{ key: '1', name: 'a' },
{ key: '7', name: 'g' },
{ key: '4', name: 'd' }
]
const diff = listDiff(oldList, newList, 'key')
console.log(diff.moves)
[{
type: 'insert',
originIndex: null,
currentIndex: 1,
item: { key: '5', name: 'e' }
}, {
type: 'insert',
originIndex: null,
currentIndex: 2,
item: { key: '6', name: 'f' }
}, {
type: 'move',
originIndex: 0,
currentIndex: 3,
item: { key: '1', name: 'a' }
}, {
type: 'insert',
originIndex: null,
currentIndex: 4,
item: { key: '7', name: 'g' }
}, {
type: 'remove',
originIndex: 2,
currentIndex: null,
item: { key: '3', name: 'c' }
}]
微信:yx12032331