当前位置:网站首页>JS to determine the added and decreased elements of two arrays
JS to determine the added and decreased elements of two arrays
2022-06-23 07:27:00 【Sister Chunfeng】
One 、 Use scenarios
1、 Compare the two arrays to increase 、 Reduced elements ;
2、 In the actual project, when the current station sends data in array format to the background , You can compare new and old data first , Only changes are sent to the background each time . Increase transmission efficiency ;
Two 、 Code
/** * Method name : * Function is introduced : Returns an object that contains an array that is greater than the first array 、 Reduced data ( Applicable to the array after de duplication ) * Parameters : * beforeArr: The previous array * afterArr: The latter array */
function compare(beforeArr,afterArr){
let resObj = {
add : [],
del : []
},
cenObj = {
};
// hold beforeArr Array de reloading cenObj
for(let i=0;i<beforeArr.length;i++){
cenObj[beforeArr[i]] = beforeArr[i];
}
// Traverse afterArr, Check whether its element is in cenObj in
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)
3、 ... and 、 Running results :

边栏推荐
- 407 stack and queue (232. implementing queue with stack, 225. implementing stack with queue)
- char和varchar区别
- User mode and kernel mode
- Spock-sub打桩
- 'Latin-1' codec can't encode characters in position 103-115: body ('string of Chinese ') is not valid Latin-1
- 301. delete invalid brackets
- 898. subarray bitwise OR operation
- yolov5检测小目标(附源码)
- 20bn Jester complete dataset Download
- MYSQL牛客刷题
猜你喜欢
随机推荐
MySQL(八) — 执行计划(Explain)详解
csrf攻击在laravel中如何解决
别找了诸位 【十二款超级好用的谷歌插件都在这】(确定不来看看?)
GINet
Project_ Filter to solve Chinese garbled code
898. subarray bitwise OR operation
[AI practice] xgb Xgbregression multioutputregressor parameter 2 (GPU training model)
'Latin-1' codec can't encode characters in position 103-115: body ('string of Chinese ') is not valid Latin-1
PSP代码实现
GINet
MySQL(五) — 锁及事务
Here comes the dry goods | PAAS collection to see first ~
传智教育 | 项目发布前如何打tag标签及标签命名规范
npm下载报错npm ERR code ERESOLVE
MySQL transaction isolation level
303. region and retrieval - array immutable
295. median data flow
900. RLE iterator
CIRIUM(睿思誉)逐渐成为航空公司二氧化碳排放报告的标准
[AI practice] data normalization and standardization of machine learning data processing








