jquery 过滤数组以根据多列删除重复记录

lrl1mhuk  于 2023-11-17  发布在  jQuery
关注(0)|答案(2)|浏览(111)

下面是我的输入数组:

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"
        }
    ]

afdcj2ne

afdcj2ne1#

使用集合获得唯一性
然后将所有相关字段链接起来

let uniqueCombinations = new Set();
let outputArr = [];

for (let item of inputArr) {
    let combination = item.baseSupplierId+ "#SOME_SEPARATOR#" + item.partNumber;

    if (!uniqueCombinations.has(combination)) {
        outputArr.push(item);
        uniqueCombinations.add(combination);
    }
}

字符串
如果集合中不存在,则插入到输出数组中。

iyfjxgzm

iyfjxgzm2#

您可以转换(使用Array.prototype.reduce())为new Map()(通过唯一属性),然后提取Map的.values()

const getUniquePartBySupplierId = (arr, id) => {
  const outputMap = inputArr.reduce((m, item) => {
    if (!m.has(item.partNumber) && item.baseSupplierId === id) {
      m.set(item.partNumber, item);
    }
    return m;
  }, new Map());
  return [...outputMap.values()];
};

const 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"},
];

const output = getUniquePartBySupplierId(inputArr, "71277");
console.log(output);

字符串

相关问题