简体   繁体   English

从现有数组创建一个新数组并将每个值连接成一个唯一值并检查是否已在新数组中设置唯一连接值

[英]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.

相关问题 使用现有数组中的唯一连接值从现有数组创建新数组 - Create new array from existing array with unique joined values from existing array 创建新的唯一值集,但从同一记录中复制另一个值以创建新的对象数组 - Create new Set of unique values but copy another value from the same record to create a new array of objects 在新数组中返回数组中值的每个唯一实例 - Return each unique instance of value in an array, in a new array 如何合并 2 个数组并创建一个具有唯一值(如 Union)的新数组? - How to merge 2 Arrays and create a new array with unique value like Union? 使用Unique和Sum从现有数组值创建新数组 - Create New Array from Existing Array Values With Unique and Sum 如何将一个数组中的值减去另一个数组并创建一个新数组? - How to subtract value from one array to another and create a new Array? 根据现有对象的属性值创建新的对象数组 object 将现有对象添加到新数组 - Create a new array of objects by property value from existing object add existing objects to new array 从数组获取唯一值 - Getting Unique value from an array JavaScript 通过过滤 object 值从现有创建新数组 - JavaScript create new Array from existing by filtering object value 仅使用包含特定值的元素从现有数组创建新数组 - Create new array from existing one only with elements that contain specific value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM