我有两个包含嵌套对象的数组。我想合并这些并获得一个独特的数组
我有这两个数组
// ARRAY 1
let variants = [
{color: "Red", sizes: "Small", material: "Cotton", price: "$100", ...},
{color: "Red", sizes: "Large", material: "Cotton", price: "$120", ...},
{color: "Blue", sizes: "Small", material: "Cotton", price: "$150", ...},
{color: "Blue", sizes: "Large", material: "Cotton", price: "$180", ...},
]
// ARRAY 2
let newVariants = [
{color: "Red", sizes: "Small", material: "Cotton"}, // this one is already exist in ARRAY 1
{color: "Red", sizes: "Large", material: "Cotton"}, // this one is already exist in ARRAY 1
{color: "Blue", sizes: "Small", material: "Cotton"}, // this one is already exist in ARRAY 1
{color: "Blue", sizes: "Large", material: "Wool"}, // this one is new object
{color: "Green", sizes: "Large", material: "Cotton"}, // this one is new object
]
我想要这个
[
{color: "Red", sizes: "Small", material: "Cotton", price: "$100"},
{color: "Red", sizes: "Large", material: "Cotton", price: "$120"},
{color: "Blue", sizes: "Small", material: "Cotton", price: "$150"},
{color: "Blue", sizes: "Large", material: "Cotton", price: "$180"},
{color: "Blue", sizes: "Large", material: "Wool", price: null, ...},
{color: "Green", sizes: "Large", material: "Cotton", price: null, ...}
]
注意:ARRAY 1 的值将始终取代 ARRAY 2
谢谢!