为了账号安全,请及时绑定邮箱和手机立即绑定

从一个对象中删除与其他对象项匹配的项

从一个对象中删除与其他对象项匹配的项

MMMHUHU 2022-10-08 17:53:21
我有一个对象结构如下"designProjects": [  {    "projectNumber": "number1",    "name": "test1"  },  {    "projectNumber": "number2",    "name": "test2"  },  {    "projectNumber": "number3",    "name": "test3"  },]我有另一个具有如下结构的对象"allProjects": [  {    "project": {      "name": "test1",      "number": "number1"    },    "employee": {      "displayName": "name1"    },    "projectRoleName": "Editor"  },  {    "project": {      "name": "test2",      "number": "number2"    },    "employee": {      "displayName": "name2"    },    "projectRoleName": "Editor"  },]我有点像下面这样的结果"designProjects": [  {    "projectNumber": "number3",    "name": "test3"  },]这里的结果 designProjects只有一个,因为项目编号和名称与对象project数组匹配allprojects。有没有办法我们可以在react js. 任何建议或想法都会非常感谢我,非常感谢提前
查看完整描述

2 回答

?
回首忆惘然

TA贡献1847条经验 获得超11个赞

您只需与 结合.filter使用.some,例如:


let d = {

  "designProjects": [

    {

      "projectNumber": "number1",

      "name": "test1"

    },

    {

      "projectNumber": "number2",

      "name": "test2"

    },

    {

      "projectNumber": "number3",

      "name": "test3"

    },

  ]

}


let a = {

  "allProjects": [

    {

      "project": {

        "name": "test1",

        "number": "number1"

      },

      "employee": {

        "displayName": "name1"

      },

      "projectRoleName": "Editor"

    },

    {

      "project": {

        "name": "test2",

        "number": "number2"

      },

      "employee": {

        "displayName": "name2"

      },

      "projectRoleName": "Editor"

    },

  ]

};


console.log(

    d.designProjects.filter((designProject) => {

        return !a.allProjects.some((project) => designProject.projectNumber === project.project.number && designProject.name === project.project.name);

     })

 );


查看完整回答
反对 回复 2022-10-08
?
慕村225694

TA贡献1880条经验 获得超4个赞

您可以结合过滤器一些

filter用于根据条件返回一个新的过滤数组

some将成为条件并在找到匹配项后立即返回

const designProjects = [{

    "projectNumber": "number1",

    "name": "test1"

  },

  {

    "projectNumber": "number2",

    "name": "test2"

  },

  {

    "projectNumber": "number3",

    "name": "test3"

  },

];


const allProjects = [{

    "project": {

      "name": "test1",

      "number": "number1"

    },

    "employee": {

      "displayName": "name1"

    },

    "projectRoleName": "Editor"

  },

  {

    "project": {

      "name": "test2",

      "number": "number2"

    },

    "employee": {

      "displayName": "name2"

    },

    "projectRoleName": "Editor"

  },

]


const cleaned = designProjects.filter((x) => {

  return !allProjects.some(y => y.project.number === x.projectNumber);

});


console.info(cleaned);


查看完整回答
反对 回复 2022-10-08
  • 2 回答
  • 0 关注
  • 116 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信