我试图通过在 sku 上进行匹配来将 array1 和 array2 合并在一起。
但是如果一个 sku 有多个(比如 62617802),我想同时保留 array1 和 array2 的值​​,
例如,第一个 sku (62617802) 将与 array2 的第一个 sku (62617802) 合并,依此类推。
输入注意:如果阵列 1 中的 SKU 重复(如两次或三次左右),则阵列 2 上的 SKU 也相同。此外,如果 array1 计数为 5,则 array2 计数也为 5。

var array1 = [
    {
        "sku": "35189424",
        "price": 107800,
        "isNew": false,
        "name": "Product Title A"
    },
    {
        "sku": "62617802",  // duplicate
        "price": 107800,
        "isNew": false,
        "name": "Product Title D"
    },
    {
        "sku": "GRP00437",
        "price": 107800,
        "isNew": false,
        "name": "Product Title B"
    },
    {
        "sku": "62617802",      // duplicate
        "price": 107800,
        "isNew": false,
        "name": "Product Title D"
    },
    {
        "sku": "35189432",
        "price": 107800,
        "isNew": false,
        "name": "Product Title YZ"
    }

];

var array2 = [
    {
        "sku": "35189424",
        "Url": "https://......",
        "rating": 2,
        "status": 0
    },
    {
        "sku": "62617802",  // duplicate
        "Url": "https://......",
        "rating": 5,
        "status": 1
    },
    {
        "sku": "GRP00437",
        "Url": "https://......",
        "rating": 2,
        "status": 1
    },
    {
        "sku": "35189432",
        "Url": "https://......",
        "rating": 3,
        "status": 1
    },
    {
        "sku": "62617802",  // duplicate
        "Url": "https://......",
        "rating": 5,
        "status": 1
    }
];


var outputArray = [
    {
        "sku": "35189424",
        "price": 107800,
        "isNew": false,
        "name": "Product Title A",
        "Url": "https://......",
        "rating": 2,
        "status": 0
    },
    {
        "sku": "62617802",      // duplicate
        "price": 107800,
        "isNew": false,
        "name": "Product Title D",
        "Url": "https://......",
        "rating": 5,
        "status": 1
    },
    {
        "sku": "GRP00437",
        "price": 107800,
        "isNew": false,
        "name": "Product Title B",
        "Url": "https://......",
        "rating": 2,
        "status": 1
    },
    {
        "sku": "62617802",  // duplicate
        "price": 107800,
        "isNew": false,
        "name": "Product Title D",
        "Url": "https://......",
        "rating": 5,
        "status": 1
    },
    {
        "sku": "35189432",
        "price": 107800,
        "isNew": false,
        "name": "Product Title YZ",
        "Url": "https://......",
        "rating": 3,
        "status": 1
    }
];

最佳答案

您可以使用一个对象对相同的 skushift 对象进行分组,以保持相同的合并顺序。

const
    array1 = [{ sku: "35189424", price: 107800, isNew: false, name: "Product Title A" }, { sku: "62617802", price: 107800, isNew: false, name: "Product Title D" }, { sku: "GRP00437", price: 107800, isNew: false, name: "Product Title B" }, { sku: "62617802", price: 107800, isNew: false, name: "Product Title D" }, { sku: "35189432", price: 107800, isNew: false, name: "Product Title YZ" }],
    array2 = [{ sku: "35189424", Url: "https://......", rating: 2, status: 0 }, { sku: "62617802", Url: "https://......", rating: 5, status: 1 }, { sku: "GRP00437", Url: "https://......", rating: 2, status: 1 }, { sku: "35189432", Url: "https://......", rating: 3, status: 1 }, { sku: "62617802", Url: "https://......", rating: 5, status: 1 }],
    skus = array2.reduce((r, o) => ((r[o.sku] = r[o.sku] || []).push(o), r), { }),
    result = array1.map(o => ({ ...o, ...skus[o.sku].shift() }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于JavaScript - 按键合并两个数组并保留重复项,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/64067567/

10-13 02:21