下面是我的输入数组:
let inputArr = [
{
"supplierId": "1",
"baseSupplierId": "71277",
"supplierName": "Supplier 1",
"partNumber": "4018992",
"partDescription": "Description 1"
},
{
"supplierId": "2",
"baseSupplierId": "71277",
"supplierName": "Supplier 2",
"partNumber": "4018992",
"partDescription": "Description 2"
},
{
"supplierId": "3",
"baseSupplierId": "71277",
"supplierName": "Supplier 3",
"partNumber": "4019030",
"partDescription": "Description 3"
},
{
"supplierId": "4",
"baseSupplierId": "71277",
"supplierName": "Supplier 4",
"partNumber": "4018291",
"partDescription": "Description 4"
},
{
"supplierId": "5",
"baseSupplierId": "71277",
"supplierName": "Supplier 5",
"partNumber": "4018992",
"partDescription": "Description 5"
}
]
字符串
我希望有一个数组作为输出,它将具有基于列“baseSupplierId”和“partNumber”的唯一值,并且如果有多个记录具有相同的“baseSupplierId”和“partNumber”,则只获取第一个并删除其他重复项。
所以我希望输出数组如下:
let outputArr = [
{
// This is the item that had multiple records with the same "baseSupplierId" and "partNumber" and now only one should come in output
"supplierId": "1",
"baseSupplierId": "71277",
"supplierName": "Supplier 1",
"partNumber": "4018992",
"partDescription": "Description 1"
},
{
"supplierId": "3",
"baseSupplierId": "71277",
"supplierName": "Supplier 3",
"partNumber": "4019030",
"partDescription": "Description 3"
},
{
"supplierId": "4",
"baseSupplierId": "71277",
"supplierName": "Supplier 4",
"partNumber": "4018291",
"partDescription": "Description 4"
}
]
型
2条答案
按热度按时间afdcj2ne1#
使用集合获得唯一性
然后将所有相关字段链接起来
字符串
如果集合中不存在,则插入到输出数组中。
iyfjxgzm2#
您可以转换(使用Array.prototype.reduce())为new Map()(通过唯一属性),然后提取Map的
.values()
:字符串