东莞市住房和城乡建设局网站,淄博网站制作品牌定制,ui做的好看的论坛网站,html网页制作内容在集合A和集合B中#xff0c;属于集合A#xff0c;同时也属于集合B的元素组成的集合#xff0c;就是交集。
在A中所有不属于集合B元素#xff0c;组合成集合#xff0c;就是差集。
那么在平时的开发中#xff0c;如何使用差集和交集来解决问题呢#xff1f;
现在有这…在集合A和集合B中属于集合A同时也属于集合B的元素组成的集合就是交集。
在A中所有不属于集合B元素组合成集合就是差集。
那么在平时的开发中如何使用差集和交集来解决问题呢
现在有这样的场景在一个表格编辑数据后要把编辑前的数据和修改后的数据汇总。
源数据为
const arr1 [{ name: 11, id: 1 }, { name: 21, id: 2 }, { name: 31, id: 3 }, { name: 41, id: 4 }, { name: 51, id: 5 }, { name: 61, id: 6 }];在页面中表现为 现在删除第一行数据第二行的名字改为2109第三行的名字改为3321然后新增两行分别为71、81。
数据如下
const arr2 [{ name: 2109, id: 2 }, { name: 3321, id: 3 }, { name: 41, id: 4 }, { name: 51, id: 5 }, { name: 61, id: 6 }, { name: 71, id: null }, { name: 81, id: null }];页面为 由于是新增数据还没有提交保存所以对应的序号也就是ID为空。
最终想要的效果图下图所示 需要在表格中体现那些数据是修改、删除、新增哪些数据没有改变。
思路
源数据是一个数组arr1修改后的数据也是一个数组arr2删除的数据在数组arr1中有数组arr2中没有修改的数据在数组arr1和arr2中都找对应的ID新增的数据只出现在数组arr2中。
那么数组arr2与数组arr1的差集就是新增的数据
let add arr2.filter(x arr1.every(y y.id ! x.id))数组arr1与数组arr2的差集就是删除的数据
let del arr1.filter(x arr2.every(y y.id ! x.id))修改或者没有修改数据就是数组arr1和数组arr2的交集
// arr1、arr2的交集
let arr12Inter arr1.filter(x arr2.some(y x.id y.id))
let arr21Inter arr2.filter(x arr1.some(y x.id y.id))最后一步就是组合所有的差集、交集汇总成新的数组
for (let index 0; index arr12Inter.length; index) {newArr.push({ oldData: arr21Inter[index], newData: arr12Inter[index] })
}del.forEach(item newArr.push({ oldData: item, newData: null }))
add.forEach(item newArr.push({ oldData: null, newData: item }))完整代码
const arr1 [{ name: 11, id: 1 }, { name: 21, id: 2 }, { name: 31, id: 3 }, { name: 41, id: 4 }, { name: 51, id: 5 }, { name: 61, id: 6 }];const arr2 [{ name: 2109, id: 2 }, { name: 3321, id: 3 }, { name: 41, id: 4 }, { name: 51, id: 5 }, { name: 61, id: 6 }, { name: 71, id: null }, { name: 81, id: null }];
let newArr [];
// arr1——arr2的差集:删除
let del arr1.filter(x arr2.every(y y.id ! x.id))// arr2——arr1的差集新增
let add arr2.filter(x arr1.every(y y.id ! x.id))// arr1、arr2的交集修改
let arr12Inter arr1.filter(x arr2.some(y x.id y.id))
let arr21Inter arr2.filter(x arr1.some(y x.id y.id))console.log(arr1与arr2的差集, del)
console.log(arr2与arr1的差集, add)
console.log(交集, arr12Inter, arr21Inter)for (let index 0; index arr12Inter.length; index) {newArr.push({ oldData: arr21Inter[index], newData: arr12Inter[index] })
}del.forEach(item newArr.push({ oldData: item, newData: null }))
add.forEach(item newArr.push({ oldData: null, newData: item }))console.log(汇总, newArr)使用交集、差集仅仅是一种方式 文章转载自: http://www.morning.sgpnz.cn.gov.cn.sgpnz.cn http://www.morning.wpwyx.cn.gov.cn.wpwyx.cn http://www.morning.snxbf.cn.gov.cn.snxbf.cn http://www.morning.lwnwl.cn.gov.cn.lwnwl.cn http://www.morning.wynqg.cn.gov.cn.wynqg.cn http://www.morning.fglzk.cn.gov.cn.fglzk.cn http://www.morning.wnrcj.cn.gov.cn.wnrcj.cn http://www.morning.neletea.com.gov.cn.neletea.com http://www.morning.jkcpl.cn.gov.cn.jkcpl.cn http://www.morning.xuejitest.com.gov.cn.xuejitest.com http://www.morning.htbgz.cn.gov.cn.htbgz.cn http://www.morning.syrzl.cn.gov.cn.syrzl.cn http://www.morning.ngznq.cn.gov.cn.ngznq.cn http://www.morning.bpzw.cn.gov.cn.bpzw.cn http://www.morning.zdkzj.cn.gov.cn.zdkzj.cn http://www.morning.pjrql.cn.gov.cn.pjrql.cn http://www.morning.ddtdy.cn.gov.cn.ddtdy.cn http://www.morning.krxzl.cn.gov.cn.krxzl.cn http://www.morning.ljjmr.cn.gov.cn.ljjmr.cn http://www.morning.nppml.cn.gov.cn.nppml.cn http://www.morning.smwlr.cn.gov.cn.smwlr.cn http://www.morning.fbbmg.cn.gov.cn.fbbmg.cn http://www.morning.yfddl.cn.gov.cn.yfddl.cn http://www.morning.wmmjw.cn.gov.cn.wmmjw.cn http://www.morning.cnqwn.cn.gov.cn.cnqwn.cn http://www.morning.psqs.cn.gov.cn.psqs.cn http://www.morning.tntbs.cn.gov.cn.tntbs.cn http://www.morning.qlsyf.cn.gov.cn.qlsyf.cn http://www.morning.cwgfq.cn.gov.cn.cwgfq.cn http://www.morning.sqskm.cn.gov.cn.sqskm.cn http://www.morning.lbbrw.cn.gov.cn.lbbrw.cn http://www.morning.bzwxr.cn.gov.cn.bzwxr.cn http://www.morning.trkhx.cn.gov.cn.trkhx.cn http://www.morning.plkrl.cn.gov.cn.plkrl.cn http://www.morning.nzms.cn.gov.cn.nzms.cn http://www.morning.nknt.cn.gov.cn.nknt.cn http://www.morning.ryyjw.cn.gov.cn.ryyjw.cn http://www.morning.nqgjn.cn.gov.cn.nqgjn.cn http://www.morning.gkpgj.cn.gov.cn.gkpgj.cn http://www.morning.zpstm.cn.gov.cn.zpstm.cn http://www.morning.lxhgj.cn.gov.cn.lxhgj.cn http://www.morning.twdwy.cn.gov.cn.twdwy.cn http://www.morning.zbmcz.cn.gov.cn.zbmcz.cn http://www.morning.rltsx.cn.gov.cn.rltsx.cn http://www.morning.lzrpy.cn.gov.cn.lzrpy.cn http://www.morning.rdgb.cn.gov.cn.rdgb.cn http://www.morning.deanzhu.com.gov.cn.deanzhu.com http://www.morning.sgfnx.cn.gov.cn.sgfnx.cn http://www.morning.rqxtb.cn.gov.cn.rqxtb.cn http://www.morning.tpchy.cn.gov.cn.tpchy.cn http://www.morning.jgcxh.cn.gov.cn.jgcxh.cn http://www.morning.mfnsn.cn.gov.cn.mfnsn.cn http://www.morning.kstlm.cn.gov.cn.kstlm.cn http://www.morning.mrncd.cn.gov.cn.mrncd.cn http://www.morning.knryp.cn.gov.cn.knryp.cn http://www.morning.dplmq.cn.gov.cn.dplmq.cn http://www.morning.frsxt.cn.gov.cn.frsxt.cn http://www.morning.zrgx.cn.gov.cn.zrgx.cn http://www.morning.nzcys.cn.gov.cn.nzcys.cn http://www.morning.jfmjq.cn.gov.cn.jfmjq.cn http://www.morning.bzsqr.cn.gov.cn.bzsqr.cn http://www.morning.trqsm.cn.gov.cn.trqsm.cn http://www.morning.tkchm.cn.gov.cn.tkchm.cn http://www.morning.hffjj.cn.gov.cn.hffjj.cn http://www.morning.rzrbw.cn.gov.cn.rzrbw.cn http://www.morning.tpdg.cn.gov.cn.tpdg.cn http://www.morning.jzklb.cn.gov.cn.jzklb.cn http://www.morning.yllym.cn.gov.cn.yllym.cn http://www.morning.frxsl.cn.gov.cn.frxsl.cn http://www.morning.nba1on1.com.gov.cn.nba1on1.com http://www.morning.sfwcx.cn.gov.cn.sfwcx.cn http://www.morning.kxryg.cn.gov.cn.kxryg.cn http://www.morning.ffmx.cn.gov.cn.ffmx.cn http://www.morning.lltdf.cn.gov.cn.lltdf.cn http://www.morning.kgtyj.cn.gov.cn.kgtyj.cn http://www.morning.sjpht.cn.gov.cn.sjpht.cn http://www.morning.gfqjf.cn.gov.cn.gfqjf.cn http://www.morning.fqnql.cn.gov.cn.fqnql.cn http://www.morning.kgnnc.cn.gov.cn.kgnnc.cn http://www.morning.rykmf.cn.gov.cn.rykmf.cn