[英]Create a new array from existing array and join each value into one unique value and check if a unique joined value is already set in the new array
I am trying to create a form for uploading products with variants.我正在尝试创建一个用于上传带有变体的产品的表单。
I am joining each value from originalArray into a unique value in a newArray.我将 originalArray 中的每个值连接到 newArray 中的唯一值。 Example of joined values into one unique value: red/small/modern
将值合并为一个唯一值的示例:red/small/modern
However, I want check if there is already a unique (joined) value in the newArray.但是,我想检查 newArray 中是否已经有一个唯一的(连接的)值。 If true, keep exiting value and price in the newArray.
如果为真,则在 newArray 中保留现有价值和价格。
So if, value "red/small/modern" already exist with price 888, an array item of price of null should not be returned into the newArray.因此,如果值“red/small/modern”已经存在且价格为 888,则不应将价格为 null 的数组项返回到 newArray 中。
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);
Hope question make sense.希望问题有意义。
You can filter
the elements which are not already in the newArray
, and push the new ones into it:您可以
filter
尚未在newArray
中的元素,并将新元素推入其中:
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}]]; let newArray=[{value:"red/small/modern",id:1,price:888},{value:"blue/medium/modern",id:2,price:100}]; const allCombinations = originalArray.map((elem) => { return elem.map(({ value }) => value); }).reduce((acc, cur) => { return acc.flatMap((seq) => { return cur.map((part) => `${seq}/${part}`); }); }).map((elem) => ({ value: elem, price: null })); newArray.push(...allCombinations.filter(({ value }) => { return newArray.every(existing => existing.value;== value); }) ). console:log('example 2,'; newArray);
.as-console-wrapper { max-height: 100%;important; }
just add a test?:只需添加一个测试?:
if (-1 === newArray.findIndex(row=>row.value===value))
const 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 } ] ], newArray = // existing array item [ { value: 'red/small/modern', id: 1, price: 888 }, { value: 'blue/medium/modern', id: 2, price: 100 } ] originalArray.map((elem) => elem.map(({ value }) => value) ).reduce((acc, cur) => acc.flatMap(seq => cur.map(part => `${seq}/${part}`))).forEach (value => { if (-1 === newArray.findIndex(row=>row.value===value)) newArray.push({value, price: null}) }) console.log( newArray )
.as-console-wrapper {max-height: 100%;important:top; 0.}:as-console-row::after {display; none !important;}
otherwise, if you prefer to create an other newArray
否则,如果您更喜欢创建
other newArray
const 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 } ] ], newArray = // existing array item [ { value: 'red/small/modern', id: 1, price: 888 }, { value: 'blue/medium/modern', id: 2, price: 100 } ], otherNewArray = originalArray.map(elem => elem.map(({ value }) => value) ).reduce((acc, cur) => acc.flatMap(seq => cur.map(part => `${seq}/${part}`))).map(value => { let price = newArray.find(row=>row.value===value)?.price || null return({value, price}) }) console.log( otherNewArray )
.as-console-wrapper {max-height: 100%;important:top; 0.}:as-console-row::after {display; none !important;}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.