简体   繁体   English

Javascript将数组元素相互组合的最有效方法

[英]Javascript most efficient way to combine array elements with each other

I'm trying to combine arrays of string with unique combinations, like 'Mark|Tom' but without 'Tom|Mark' 我正在尝试将字符串数组与唯一的组合(例如“ Mark | Tom”,但不包含“ Tom | Mark”)结合起来

I have written this code: 我写了这段代码:

 let arr = ['Tom', 'Danny', 'Mark'] let sets = [] for (let i = 0; i < arr.length; i++) { let others = arr.filter(name => name != arr[i]) others.forEach((other) => { let newel = arr[i] + '|' + other let test = newel.split('|') if (sets.includes(test[1] + '|' + test[0]) || sets.includes(newel)) return sets.push(newel) }) } console.log(sets) 

This is iterating through every array element and then creating another array of other elements from basic array and then iterating through them (again iteration), creating a combination, checking if there is reversed combination of our elements (if this was created in previous loops) and if there is no such combination = push it to target array. 这是遍历每个数组元素,然后从基本数组创建其他元素的另一个数组,然后遍历它们(再次迭代),创建一个组合,检查我们的元素是否存在反向组合(如果这是在先前的循环中创建的)如果没有这样的组合=将其推入目标数组。

Is there more elegant way to do that task? 有没有更优雅的方式来完成这项任务?

what about this one: 这个如何:

 const arr = ['Tom', 'Danny', 'Mark']; const sets = []; for (let i = 0; i < arr.length; i++) { for (let j = i + 1; j < arr.length; j++) { sets.push(arr[i] + '|' + arr[j]); } } console.log(sets); 

Also you can make arr unique before start the calculate: 您也可以在开始计算之前使arr唯一:

const arr = [...new Set(['Tom', 'Danny', 'Mark', 'Tom'])]; // ['Tom', 'Danny', 'Mark']

I would make your array a set so it will remove all duplicates initially like so: 我会将您的数组设为一个集合,这样一来,它将像这样删除所有重复项:

new Set(['Tom', 'Danny', 'Mark']);

Now, you can pair the i th element with all elements up to the n th element using .flatMap and .reduce like so: 现在,您可以使用.flatMap.flatMap .reduce n th i th元素与所有元素配对直到i th n th元素,如下所示:

 const name_arr = Array.from(new Set(['Tom', 'Danny', 'Mark'])); const res = name_arr.flatMap( (name, i) => name_arr.slice(i+1).reduce((a, n) => [...a, name+'|'+n],[]) ); console.log(res); 

You can create a Set of passed array, loop through the array, for each index get the array after that index and loop thorough this other array and build combinations 您可以创建一个传递数组的集合,遍历该数组,为每个索引获取该索引之后的数组,然后循环遍历另一个数组并构建组合

 let arr = ['Tom', 'Danny', 'Mark'] const uniqueCombo = (arr) => { let newSet = [...new Set(arr)] return newSet.reduce((op, inp, index) => { newSet.slice(index + 1,).forEach(v => { op.push(inp + '|' + v) }) return op },[]) } console.log(uniqueCombo(arr)) console.log(uniqueCombo(['A', 'B', 'A', 'C', 'D'])) console.log(uniqueCombo(['A', 'B', 'A', 'C'])) 

Try this one. 试试这个。 This approach filters out the index which has been previously used to push item to array. 这种方法可以过滤掉以前用于将项目推入数组的索引。

 const arr = ['Tom', 'Danny', 'Mark']; const comb = []; for (let i = 0; i < arr.length; i++) { for (j = i + 1; j < arr.length; j++) { comb.push(arr[i] + '|' + arr[j]); } } console.log(comb); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM