Javascript - 在单个数组中生成元素的所有组合(成对) [英] Javascript - Generating all combinations of elements in a single array (in pairs)

查看:179
本文介绍了Javascript - 在单个数组中生成元素的所有组合(成对)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了几个关于如何在数组中生成所有可能的元素组合的类似问题。但是我很难搞清楚如何编写一个只能输出组合的算法。任何建议都将受到超级赞赏!



从以下数组开始(包含N个元素):

  var array = [apple,banana,lemon,mango]; 

获得以下结果:

  var result = [
apple banana
apple lemon
apple mango
banana lemon
香蕉芒果
柠檬芒果
];

我正在尝试以下方法,但这会导致所有可能的组合,而不是组合对。

  var letters = splSentences; 
var combi = [];
var temp =;
var letLen = Math.pow(2,letters.length);

for(var i = 0; i< letLen; i ++){
temp =;
for(var j = 0; j< letters.length; j ++){
if((i& Math.pow(2,j))){
temp + = letters [ j] +
}
}
if(temp!==){
combi.push(temp);
}
}


解决方案

A简单的方法是在数组中执行double for循环,跳过第二个循环中的第一个 i 元素。



< pre class =snippet-code-js lang-js prettyprint-override> let array = [apple,banana,lemon,mango]; let results = []; //因为你只想要成对,所以没有理由//直接迭代最后一个元素(让i = 0; i< array.length - 1; i ++){//这是你捕获最后一个值的地方for(let j = i + 1; j< array.length; j ++){results.push(`$ {array [i]} $ {array [j]}`); console.log(results);



用ES5重写:



  var array = [apple,banana,lemon ,mango]; var results = []; //因为你只想要成对,所以没有理由直接迭代最后一个元素(var i = 0; i< array.length  -  1; i ++){ //这是你将捕获最后一个值(var j = i + 1; j< array.length; j ++){results.push(array [i] +''+ array [j]); console.log(results);  


I've seen several similar questions about how to generate all possible combinations of elements in an array. But I'm having a very hard time figuring out how to write an algorithm that will only output combination pairs. Any suggestions would be super appreciated!

Starting with the following array (with N elements):

var array = ["apple", "banana", "lemon", "mango"];

And getting the following result:

var result = [
   "apple banana"
   "apple lemon"
   "apple mango"
   "banana lemon"
   "banana mango"
   "lemon mango"
];

I was trying out the following approach but this results in all possible combinations, instead only combination pairs.

var letters = splSentences;
var combi = [];
var temp= "";
var letLen = Math.pow(2, letters.length);

for (var i = 0; i < letLen ; i++){
    temp= "";
    for (var j=0;j<letters.length;j++) {
        if ((i & Math.pow(2,j))){ 
            temp += letters[j]+ " "
        }
    }
    if (temp !== "") {
        combi.push(temp);
    }
}

解决方案

A simple way would be to do a double for loop over the array where you skip the first i elements in the second loop.

let array = ["apple", "banana", "lemon", "mango"];
let results = [];

// Since you only want pairs, there's no reason
// to iterate over the last element directly
for (let i = 0; i < array.length - 1; i++) {
  // This is where you'll capture that last value
  for (let j = i + 1; j < array.length; j++) {
    results.push(`${array[i]} ${array[j]}`);
  }
}

console.log(results);

Rewritten with ES5:

var array = ["apple", "banana", "lemon", "mango"];
var results = [];

// Since you only want pairs, there's no reason
// to iterate over the last element directly
for (var i = 0; i < array.length - 1; i++) {
  // This is where you'll capture that last value
  for (var j = i + 1; j < array.length; j++) {
    results.push(array[i] + ' ' + array[j]);
  }
}

console.log(results);

这篇关于Javascript - 在单个数组中生成元素的所有组合(成对)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆