当前位置:网站首页>js 判断两个数组增加和减少的元素
js 判断两个数组增加和减少的元素
2022-06-23 06:06:00 【春风姐姐】
一、使用场景
1、对比两个数组增加、减少的元素;
2、实际项目中当前台向后台发送数组格式的数据时,可以先进行新老数据对比,每次只向后台发送改变的。增加传送效率;
二、代码
/** * 方法名: * 功能介绍:返回一个对象里面包含后一个数组比第一个数组增加、减少的数据(适用于去重过后的数组) * 参数: * beforeArr:前一个数组 * afterArr:后一个数组 */
function compare(beforeArr,afterArr){
let resObj = {
add : [],
del : []
},
cenObj = {
};
//把beforeArr数组去重放入cenObj
for(let i=0;i<beforeArr.length;i++){
cenObj[beforeArr[i]] = beforeArr[i];
}
//遍历afterArr,查看其元素是否在cenObj中
for (let j=0;j<afterArr.length;j++){
if (!cenObj[afterArr[j]]){
resObj.add.push(afterArr[j]);
}else {
delete cenObj[afterArr[j]]
}
}
for (k in cenObj){
resObj.del.push(k);
}
return resObj;
}
var beArr = [1,2,3,4],
afArr = [2,3,6];
compare(beArr,afArr)
三、运行结果:

边栏推荐
猜你喜欢

Children's programming for comprehensively cultivating students' mental thinking

Media industry under the epidemic situation, small program ecology driven digital transformation exploration

MySQL重做日志 redo log

How to realize video call and live interaction in a small program when live broadcasting is so popular?

994. 腐烂的橘子-非递归法

EndNote20使用教程分享(未完

【BULL中文文档】用于在 NodeJS 中处理分布式作业和消息的队列包

XML DTD record

Easy EDA learning notes 09 esp32-wroom-32e module esp32-devkitc-v4 development board one click download circuit

数据在内存中的存储方式(C语言)
随机推荐
Intentional shared lock, intentional exclusive lock and deadlock of MySQL
【STL】pair用法总结
xml dtd 记录
Some difficulties in making web pages
【STL】关联容器之map用法总结
Mysql5.6 (5.7-8) is based on shardingsphere5.1.1 sharding proxy mode. Read / write separation
/bin/sh no such file or directory问题
The illustration shows three handshakes and four waves. Xiaobai can understand them
English grammar_ Adverb - ever / once
Termux
js 动态创建a href 循环下载文件只能下载10个或者固定数目的问题
Sword finger offer 42 Maximum sum of successive subarrays
Detailed explanation of callback function
Network architecture from Wan to sd-wan edge devices
Eureka
core. What is JS ---kalrry
Usage Summary of item views and item widgets controls in QT
JS dynamically creates a href circular download file. Only 10 or a fixed number of files can be downloaded
页面嵌入iframe 点击浏览器后退问题
mysql 索引