代码之家  ›  专栏  ›  技术社区  ›  Glory Raj

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

  •  0
  • Glory Raj  · 技术社区  · 4 年前

    "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 回复  |  直到 4 年前
        1
  •  2
  •   dave    4 年前

    你只需要结合 .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);
         })
     );
        2
  •  1
  •   Samuel Goldenbaum    4 年前

    你可以合并 filter some

    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);