2016-03-02 69 views
1

我想計算數組中單個元素內的笛卡爾積。一次只能有2個值。
所以,如果這是我的數組是:計算1個數組中元素的笛卡爾積

[["cat dog mouse"], ["blue red green"]] 

的預期值是:

  • 貓,狗
  • 貓,小鼠
  • 狗,小鼠
  • 藍色,紅色
  • 藍色,綠色
  • 紅色,綠色

這是我的車的做法:

var arr = [["cat dog mouse"], ["blue red green"], ["apple orange banana"]]; 

for (i = 0; i < arr.length; i++) { 
    for(j = 0; j < arr[i].length; j++){ 
     if(j >= arr[i].length){ 
      console.log(arr[i][j].split(" ") + " " + arr[i][0]) 
     }else{ 
      console.log(arr[i][j].split(" ") + " " + arr[i][j+1]) 
     } 
    } 
} 

它給了我

  • 貓,狗,老鼠不確定
  • 藍色,紅色,綠色不確定

回答

0

你可以使用做類似的事情for

var arr = [ 
 
    ["cat dog mouse"], 
 
    ["blue red green"], 
 
    ["apple orange banana"] 
 
]; 
 

 
// iterate the array 
 
for (i = 0; i < arr.length; i++) { 
 
    // split the string 
 
    var inArr = arr[i][0].split(' '); 
 
    // iterate the splitted string array 
 
    for (j = 0; j < inArr.length - 1; j++) { 
 
    // iterate string next to current string 
 
    for (k = j + 1; k < inArr.length; k++) 
 
     // generate the output 
 
     console.log(inArr[j] + ', ' + inArr[k]); 
 
    } 
 
}

0

你接近,一些修改後的代碼

var arr = [["cat dog mouse"], ["blue red green"], ["apple orange banana"]]; 

for (i = 0; i < arr.length; i++) { 
    var items = arr[i][0].split(" ") 
    for (j = 0; j < items.length - 1; j++) { 
     for (k = j+1; k < items.length; k++) { 
      console.log(items[j], items[k]) 
     } 
    } 
} 

結果:

cat dog 
cat mouse 
dog mouse 
blue red 
blue green 
red green 
apple orange 
apple banana 
orange banana 
0
[["cat dog mouse"], ["blue red green"]].forEach(function (item) { 
    item.forEach(function (value) { 
     var arr = value.split(' '); 
     var hash = {}; 
     arr.forEach(function (firstItem) { 
       arr.forEach(function (secondItem) { 
        if (firstItem !== secondItem && !hash[firstItem + secondItem] && !hash[secondItem + firstItem]) { 
         console.log(firstItem + ', ' + secondItem); 
         hash[firstItem+secondItem] = true; 
        } 
       }); 
     }) 
    }); 
}); 
0

解決方案在部分解決方案中分裂,一個用於數組,另一個用於組合,無需重複。

function combine(array, length) { 
 
    function c(l, r) { 
 
     var ll = l.slice(); 
 
      if (r.length === length) { 
 
      result.push(r); 
 
      return; 
 
     } 
 
     while (ll.length) { 
 
      c(ll, r.concat(ll.shift())); 
 
     } 
 
    } 
 
    var result = []; 
 
    c(array, []); 
 
    return result; 
 
} 
 

 
function get(array) { 
 
    return array.map(function (a) { 
 
     return combine(a[0].split(' '), 2); 
 
    }); 
 
} 
 

 
var result = get([["cat dog mouse"], ["blue red green"], ["apple orange banana"]]); 
 
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

0

你可以做到這一點在功能性的方式,只是用.MAP().forEach()方法:

var arr = [["cat dog mouse"], ["blue red green"], ["apple orange banana"]]; 


function print(item){ 
    //If there is one last item we stop the process 
    if (item.length <= 1) return; 
    else { 
    //Retrieve head of our stack and shift the first item 
    const head = item.shift(); 
    item.forEach((elm) => console.log(head + ',' + elm)); 
    // call our recursive function with the new array 
    return print(item); 
    } 
} 

function combine(arr){ 
    //Return array splited by using .map and .split operations, then iterate by using forEach and call our 
    // recursive function 
    arr 
    .map((array) => array[0].split(' ')) 
    .forEach((value) => print(value)); 
} 

combine(arr); 

您可以檢查Plunker