I have two arrays containing nested objects. I'd like to merge those and get a unique array
I have these two arrays
// 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 ] and I want this
[ {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, ...} ] Note: ARRAY 1's value will always supersede on ARRAY 2
Thanks!
https://stackoverflow.com/questions/66523595/merge-array-of-objects-and-get-unique-values March 08, 2021 at 10:26AM
没有评论:
发表评论