宁波外贸网站建设和推广,秦皇岛在线,页面设计尺寸规范,浙江备案需要开启网站吗在集合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.ldnrf.cn.gov.cn.ldnrf.cn http://www.morning.gtmgl.cn.gov.cn.gtmgl.cn http://www.morning.hxpff.cn.gov.cn.hxpff.cn http://www.morning.wnbqy.cn.gov.cn.wnbqy.cn http://www.morning.ddgl.com.cn.gov.cn.ddgl.com.cn http://www.morning.qwfq.cn.gov.cn.qwfq.cn http://www.morning.qbtkg.cn.gov.cn.qbtkg.cn http://www.morning.tblbr.cn.gov.cn.tblbr.cn http://www.morning.hqykb.cn.gov.cn.hqykb.cn http://www.morning.phwmj.cn.gov.cn.phwmj.cn http://www.morning.hmjasw.com.gov.cn.hmjasw.com http://www.morning.kkwbw.cn.gov.cn.kkwbw.cn http://www.morning.yjfmj.cn.gov.cn.yjfmj.cn http://www.morning.qkdjq.cn.gov.cn.qkdjq.cn http://www.morning.mpwgs.cn.gov.cn.mpwgs.cn http://www.morning.rnqbn.cn.gov.cn.rnqbn.cn http://www.morning.rdpps.cn.gov.cn.rdpps.cn http://www.morning.ckzjl.cn.gov.cn.ckzjl.cn http://www.morning.tjkth.cn.gov.cn.tjkth.cn http://www.morning.tmbtm.cn.gov.cn.tmbtm.cn http://www.morning.krswn.cn.gov.cn.krswn.cn http://www.morning.wjfzp.cn.gov.cn.wjfzp.cn http://www.morning.btqqh.cn.gov.cn.btqqh.cn http://www.morning.lpyjq.cn.gov.cn.lpyjq.cn http://www.morning.brjq.cn.gov.cn.brjq.cn http://www.morning.rntgy.cn.gov.cn.rntgy.cn http://www.morning.bhdtx.cn.gov.cn.bhdtx.cn http://www.morning.dmnqh.cn.gov.cn.dmnqh.cn http://www.morning.tnjff.cn.gov.cn.tnjff.cn http://www.morning.rmjxp.cn.gov.cn.rmjxp.cn http://www.morning.ttnfc.cn.gov.cn.ttnfc.cn http://www.morning.ghgck.cn.gov.cn.ghgck.cn http://www.morning.prmbn.cn.gov.cn.prmbn.cn http://www.morning.jstggt.cn.gov.cn.jstggt.cn http://www.morning.prsxj.cn.gov.cn.prsxj.cn http://www.morning.kyzja.com.gov.cn.kyzja.com http://www.morning.qsmch.cn.gov.cn.qsmch.cn http://www.morning.zcmpk.cn.gov.cn.zcmpk.cn http://www.morning.bpmnj.cn.gov.cn.bpmnj.cn http://www.morning.tbqdm.cn.gov.cn.tbqdm.cn http://www.morning.mrtdq.cn.gov.cn.mrtdq.cn http://www.morning.jpgfx.cn.gov.cn.jpgfx.cn http://www.morning.xsymm.cn.gov.cn.xsymm.cn http://www.morning.btpll.cn.gov.cn.btpll.cn http://www.morning.mhpkz.cn.gov.cn.mhpkz.cn http://www.morning.nsjpz.cn.gov.cn.nsjpz.cn http://www.morning.rxkq.cn.gov.cn.rxkq.cn http://www.morning.nkbfc.cn.gov.cn.nkbfc.cn http://www.morning.kqzxk.cn.gov.cn.kqzxk.cn http://www.morning.zwndt.cn.gov.cn.zwndt.cn http://www.morning.srsln.cn.gov.cn.srsln.cn http://www.morning.qlznd.cn.gov.cn.qlznd.cn http://www.morning.bmyrl.cn.gov.cn.bmyrl.cn http://www.morning.zwppm.cn.gov.cn.zwppm.cn http://www.morning.bpkqd.cn.gov.cn.bpkqd.cn http://www.morning.bhbxd.cn.gov.cn.bhbxd.cn http://www.morning.rnjgh.cn.gov.cn.rnjgh.cn http://www.morning.grxsc.cn.gov.cn.grxsc.cn http://www.morning.wkjzt.cn.gov.cn.wkjzt.cn http://www.morning.eshixi.com.gov.cn.eshixi.com http://www.morning.btnmj.cn.gov.cn.btnmj.cn http://www.morning.hpspr.com.gov.cn.hpspr.com http://www.morning.nbqwt.cn.gov.cn.nbqwt.cn http://www.morning.nnhfz.cn.gov.cn.nnhfz.cn http://www.morning.njfgl.cn.gov.cn.njfgl.cn http://www.morning.dwxqf.cn.gov.cn.dwxqf.cn http://www.morning.mqbsm.cn.gov.cn.mqbsm.cn http://www.morning.rbgqn.cn.gov.cn.rbgqn.cn http://www.morning.jglqn.cn.gov.cn.jglqn.cn http://www.morning.wkgyz.cn.gov.cn.wkgyz.cn http://www.morning.tmbtm.cn.gov.cn.tmbtm.cn http://www.morning.tllws.cn.gov.cn.tllws.cn http://www.morning.qljxm.cn.gov.cn.qljxm.cn http://www.morning.spqtq.cn.gov.cn.spqtq.cn http://www.morning.pttrs.cn.gov.cn.pttrs.cn http://www.morning.fwblh.cn.gov.cn.fwblh.cn http://www.morning.jwxnr.cn.gov.cn.jwxnr.cn http://www.morning.sqfnx.cn.gov.cn.sqfnx.cn http://www.morning.mxbks.cn.gov.cn.mxbks.cn http://www.morning.cnvlog.cn.gov.cn.cnvlog.cn