这篇文章主要介绍“es6和js怎么匹配两个数组对象”,在日常操作中,相信很多人在es6和js怎么匹配两个数组对象问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”es6和js怎么匹配两个数组对象”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
判断两个数组用的value是否相等
this.list = [ { user_type: 0, user_id: 1003, department_id: 1, department_name: "公司xx", mobile: "", realname: "廖xx", com_name: "任公司", label: "廖建平", value: 1003 }, { user_type: 0, user_id: 1004, department_id: 1, department_name: "公司领导", mobile: "", realname: "贺金生", com_name: "任公司", label: "贺xx", value: 1004 }, { user_type: 0, user_id: 1005, department_id: 1, department_name: "公司领导", mobile: "", realname: "李欢", com_name: "任公司", label: "李xx", value: 1005 } ] this.selectData = [ { user_type: 0, user_id: 1003, department_id: 1, department_name: "公司xx", mobile: "", realname: "廖xx", com_name: "任公司", label: "廖建平", value: 1003 }, { user_type: 0, user_id: 1004, department_id: 1, department_name: "公司领导", mobile: "", realname: "贺金生", com_name: "任公司", label: "贺xx", value: 1004 }, ]
方法一
let result = [] for (let i = 0; i < this.selectData.length; i++) { let obj = this.selectData[i] for (let j = 0; j < this.list.length; j++) { let aj = this.list[j] if (obj.value === aj.value) { result.push(aj) break } } } console.log("result", result)
方法二
let arr3 = this.selectData.filter(obj => this.list.some(obj1 => obj.value == obj1.value) ) console.info("arr3", arr3) this.result = arr3
方法三
let arr4 = [] let arr5 this.list.filter(obj => arr4.push(obj.value)) arr5 = this.selectData.filter(obj => arr4.indexOf(obj.value) !== -1) this.result = arr5
到此,关于“es6和js怎么匹配两个数组对象”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。