I am trying to create a form for uploading products with variants.
And regarding this, I am returning joined values into a new array from an existing array. However, I want check if there is already a (joined) value and price in the newArray. If true, keep exiting value and price in the newArray.
So if, value "red/small/modern" already exist with price 888, an array item of price of null should not be returned.
let originalArray = [
[
{ value: 'red', id: 99, price: null },
{ value: 'blue', id: 100, price: null },
],
[
{ value: 'small', id: 101, price: null },
{ value: 'medium', id: 102, price: null },
],
[
{ value: 'modern', id: 103, price: null },
{ value: 'classic', id: 104, price: null },
],
];
//
// existing array item
let newArray = [
{ value: 'red/small/modern', id: 1, price: 888 },
{ value: 'blue/medium/modern', id: 2, price: 100 },
];
//
console.log('example 1:', newArray); // [{…}, {…}]
//
newArray = originalArray
.map((elem) => {
return elem.map(({ value }) => value);
})
.reduce((acc, cur) => {
return acc.flatMap((seq) => {
return cur.map((part) => `${seq}/${part}`);
});
})
.map((elem) => {
return { value: elem, price: null };
});
//
//
// price for value "red/small/modern" and "blue/medium/modern" should not be null, as they are already set in the newArray
console.log('example 2:', newArray);
.as-console-wrapper {max-height: 100% !important;top: 0;}
.as-console-row::after {display: none !important;}
Hope question make sense.