-3

我有两个对象数组

const a = [
  {
    "categoryId": 1,
    "categoryName": "category 1",
    "id": 11
  },
  {
    "categoryId": 2,
    "teamName": "category 2",
    "id": 22
  }
]

const b = [
  {
    "categoryId": 2,
    "categoryName": "category 2",
    "id": 33
  },
  {
    "categoryId": 3,
    "categoryName": "category 3",
    "id": 44
  }
]

现在我想将它们合并在一起,最终结果应该如下所示:

const result = [
  {
    "categoryId": 1,
    "categoryName": "category 1",
    "aId": 11,
    "bId" null: 
  },
  {
    "categoryId": 2,
    "categoryName": "category 2",
    "aId": 22,
    "bId" 33:
  },
  {
    "categoryId": 3,
    "categoryName": "category 3",
    "aId": null,
    "bId" 44:
  }
]

我看不到如何分配空值。

我已经用 Object.assign 和 "..." 运算符尝试过,但没有得到想要的结果。

同样,我想根据数组将 id 重命名为新键。

let result = a.concat(b) 会给我一个错误的结果

4

3 回答 3

2

您可以使用contat()将数组合并在一起,然后reduce()通过其键删除重复的对象:

    const admin = [
      {
        "categoryId": 66,
        "categoryName": "category 66",
        "id": 204
      },
      {
        "categoryId": 149,
        "teamName": "category 149",
        "id": 178
      }
    ]

    const member = [
      {
        "categoryId": 66,
        "teamName": "category 66",
        "id": 271
      },
      {
        "categoryId": 68,
        "teamName": "category 68",
        "id": 264
      }
    ];
    
    const client = [
      {
        "categoryId": 66,
        "teamName": "category 66",
        "id": 271
      },
      {
        "categoryId": 155,
        "teamName": "no team",
        "id": 264
      }
    ];
    const merge = function(...params) {
        let mergeResult = [];
        params.map(elem => {mergeResult = mergeResult.concat(elem); });
        return mergeResult;
    }

    const result = Object.values(merge(member, admin, client).reduce((acc,cur)=>Object.assign(acc,{[cur.categoryId]:cur}),{}));

    console.log(result);

如果您需要更改重复的过滤条件,只需cur.categoryIdObject.assign()函数中更改

于 2019-01-22T09:33:48.280 回答
2

您可以使用 aSet来获取唯一的 id,然后您可以对其进行映射,找到匹配的数据并以所需的格式构建对象:

const admin = [{
    "categoryId": 66,
    "categoryName": "category 66",
    "id": 204
  },
  {
    "categoryId": 149,
    "teamName": "category 149",
    "id": 178
  }
]

const member = [{
    "categoryId": 66,
    "teamName": "category 66",
    "id": 271
  },
  {
    "categoryId": 68,
    "teamName": "category 68",
    "id": 264
  }
]

const findCategory = (categories, category) => categories
  .find(x => x.categoryId === category) || null;

const mergeCategories = (adminIn, memberIn) => {
  const mergedCategories = [
    ...adminIn,
    ...memberIn,
  ];
  
  // we would like to know just the unique id's
  const categoryIds = [...new Set(mergedCategories.map(x => x.categoryId))];
  
  // we can map each unique id
  return categoryIds.map(categoryId => {
  
    // then we can find whether it has a matching category for each
    const adminCategory = findCategory(admin, categoryId);
    const memberCategory = findCategory(member, categoryId);
  
    // now we can use that data to build the object in the required format
    return {
      categoryId,
      categoryName: `category ${categoryId}`,
      adminId: adminCategory === null ? adminCategory : adminCategory.id,
      memberId: memberCategory === null ? memberCategory : memberCategory.id
    }
  });

}

const result = mergeCategories(
  admin,
  member,
)

console.dir(result)

于 2019-01-22T09:45:26.130 回答
1

forEach可以使用循环,我们可以相应地分配值

const admin = [
    {
      "categoryId": 66,
      "categoryName": "category 66",
      "id": 204
    },
    {
      "categoryId": 149,
      "teamName": "category 149",
      "id": 178
    }
  ]
  
  const member = [
    {
      "categoryId": 66,
      "teamName": "category 66",
      "id": 271
    },
    {
      "categoryId": 68,
      "teamName": "category 68",
      "id": 264
    }
  ]
var arr=[];
  var k=admin.forEach((e)=>{
    var a=e.categoryId;
   if(e.teamName)
   {
e.categoryName=e.teamName;
delete e.teamName;
   }
   if(e.id)
e.adminid=e.id;
else
e.id=null
e.memberId=null;
delete e.id
var t=member.forEach((x)=>{
  if(x.categoryId==a)
  e.memberId=x.id;

})
arr.push(e);
  })
console.log(arr)

于 2019-01-22T09:47:22.743 回答