×

前端开发es6应用:匹配两个数组对象的方法

作者:abc1232022.03.29来源:Web前端之家浏览:3616评论:0
关键词:jses6

前端开发es6应用:匹配两个数组对象的方法。

判断两个数组用的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

试试吧。

您的支持是我们创作的动力!
温馨提示:本文作者系 ,经Web前端之家编辑修改或补充,转载请注明出处和本文链接:
https://www.jiangweishan.com/article/js20220329a1.html

网友评论文明上网理性发言 已有0人参与

发表评论: