js两个对象数组的lodash去重与取差集(保留不重复的元素)& js两个字符串数组取差集

js两个字符串数组取差集(保留不重复的元素)
 
const selfList = allStringList.filter(
                (itemAll: string) =>
                  notSelfList.findIndex((itemNot: string) => itemAll === itemNot) === -1,
              );

  

selfList为两个数组的差集

 
js两个对象数组的lodash去重与取差集(保留不重复的元素)
 
lodash去重:
 
 
1.引入
import _ from 'lodash';
 
2.代码:[此处以id来作为去重的判断]
let data = [...arrA, ...arrB];
data = _.uniqBy(data, 'id');
console.log(data)

  

 

取差集:

[生成一个arrA中不存在arrB中所有元素的数组]

 

 const { arrA, arrB} = this.state;
    const checkedId = arrB&& arrB.map((item: any) => item.id)
    const restList = arrA.filter((item: any) => checkedId.indexOf(item.id) === -1) || []

  

posted @ 2021-08-19 14:06  芝麻小仙女  阅读(4842)  评论(0编辑  收藏  举报