1、方法:
function getData(arr1, arr2, type) { var result = []; for (var i = 0; i < arr1.length; i++) { var item1 = arr1[i], found = false; for (var j = 0; j < arr2.length; j++) { if (item1.id === arr2[j].id) { found = true; break; } } if (found === type) { result.push(item1); } } return result; }
2、数组数据
var arr1 = [
{id:1,name:'中国'},
{id:2,name:'美国'},
{id:3,name:'英国'},
{id:4,name:'法国'},
{id:5,name:'德国'}
];
var arr2 = [
{id:1,name:'中国'},
{id:3,name:'英国'},
{id:4,name:'法国'},
{id:6,name:'西班牙'}
];
3、输出结果
console.log('获取相同数组:', getData(arr1, arr2, true));
//获取相同数组:
(3) [{…}, {…}, {…}]0: {id: 1, name: '中国'}1: {id: 3, name: '英国'}2: {id: 4, name: '法国'}]
console.log('获取不同数据:', getData(arr1, arr2, false));
//获取不同数据:
(2) [{…}, {…}]0: {id: 2, name: '美国'}1: {id: 5, name: '德国'}]
上面是“js获取2个数组中的相同数据和不同数组”的全面内容,想了解更多关于 js 内容,请继续关注web建站教程。
当前网址:https://ipkd.cn/webs_2002.html
workflows工作流
树上站着一只鸟ComfyUI工作流
一个穿过泥泞雷区的士兵ComfyUI工作流
1个很漂亮的单身小女孩ComfyUI工作流
森林里有一个皮肤像抛光黑曜石的生物
一条精致透明的朱红色水晶龙
文生图工作流:一幅海底睡莲,碧海蓝天comfyui工
一条色彩斑斓的超现实小孔雀鱼ComfyUI工作流
爱因斯坦在做实验3d动漫ComfyUI工作流
猜你喜欢
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

如何利用css3+js做一个下雨效果
利用css绘画棋盘布局(象棋)
制作一个好玩的倒计时
用ascii字符画图像
css3搭积木叠加图形
css3实现星球旋转
3d空间行走效果
纯css制作卡通头像(随鼠标转头)










