2017-02-11 13 views
4

我試着在堆棧溢出中查找類似的問題,但它並沒有滿足我的確切需求試圖做,我一直試圖讓它工作,但不斷得到錯誤的結果。在這一點上,我只是無所適從。我的意思JavaScript函數,需要一個多維數組和單個數組,並且發現多維數組中的單個數組的匹配

var masterArray = [[1,2,5,6],[5,13,7,8],[9,11,13,15],[13,14,15,16],[1,9,11,12]] 
var compareArray = [9,11,13,15,1,2,5,6] 

function intersect(a, b) { 
//code to compare both arrays and find a match 
} 

console.log(Intersect(compareArray, masterArray)) 

和輸出將

[9,11,13,15] 
[1,2,5,6] 
[5,13] 
[13,15] 
+0

展(添加到您的問題),你最大的努力,到目前爲止,並解釋地方,爲什麼你被卡住。 –

+0

再次檢查輸出!我想你忘了一個! –

+3

爲什麼'[1,9,11]'不是最終結果? – RomanPerekhrest

回答

0

按照您的邏輯例如,你有你的問題的輸出錯誤:

var masterArray = [[1,2,5,6],[5,13,7,8],[9,11,13,15],[13,14,15,16],[1,9,11,12]] 
var compareArray = [9,11,13,15,1,2,5,6] 


function intersect(a, b) { 
    var temp = []; 
    k = 0; 
    for(i = 0; i < b.length; i++){ 
     temp1 = [] 
     for(j=0;j<b[i].length;j++){ 
     if($.inArray(b[i][j], a) > -1){ 
      temp1.push(b[i][j]); 

     } 
     } 
     temp.push(temp1); 
    } 
    return temp; 
} 

console.log(intersect(compareArray, masterArray)); 

檢查這個jsfiddle

試試看,這應該有效。

1

使用Array.prototype.reduce得到所有的交叉點的數組這樣的:

var masterArray = [[1,2,5,6],[5,13,7,8],[9,11,13,15],[13,14,15,16],[1,9,11,12]]; 
 
var compareArray = [9,11,13,15,1,2,5,6]; 
 

 

 
function intersect(multi, simple) { 
 
    return multi.reduce(function(res, b) { 
 
    var intersection = simple.reduce(function(r, e) { // get the intersection of the current array and compareArray (simple) 
 
     if(b.indexOf(e) != -1) // if the current element of the current array is also in the compareArray then push it into the intersection array 
 
     r.push(e); 
 
     return r; 
 
    }, []); 
 
    
 
    res.push(intersection); // push the intersection array into the result array 
 
    return res; 
 
    }, []); 
 
} 
 

 
console.log(intersect(masterArray, compareArray));

1

使用具有特定的正則表達式模式RegExp對象將溶液(將compareArray數成正則表達式交替組項目)。使用
附加功能: String.prototype.match()Array.prototype.join()Array.prototype.map()

var masterArray = [[1,2,5,6],[5,13,7,8],[9,11,13,15],[13,14,15,16],[1,9,11,12]], 
 
    compareArray = [9,11,13,15,1,2,5,6]; 
 

 
function intersect(compareArray, masterArray) { 
 
    var pattern = new RegExp('\\b(' + compareArray.join('|') + ')\\b', 'g'), 
 
     result = []; 
 

 
    masterArray.forEach(function(v) { 
 
     var matches = v.join(' ').match(pattern); 
 
     if (matches.length) result.push(matches.map(Number)); 
 
    }); 
 

 
    return result; 
 
} 
 

 
console.log(intersect(compareArray, masterArray));

0

您可以使用此功能ES6:

function intersect(a, b) { 
 
    a = new Set(a); // for faster lookup 
 
    return b.map(c => c.filter(a.has.bind(a))).filter(Boolean); 
 
} 
 
// Demo 
 
const masterArray = [[1,2,5,6],[5,13,7,8],[9,11,13,15],[13,14,15,16],[1,9,11,12]]; 
 
const compareArray = [9,11,13,15,1,2,5,6]; 
 
console.log(intersect(compareArray, masterArray).join('\n'));

.filter(Boolean)部分是可選的,只需要排除根本沒有匹配數字的數組,否則將在結果中由空數組表示。

使用Set將提高性能,因爲在Set(含has)中的查找可以在接近恆定的時間內完成。

0

您可以映射masterArray的過濾結果。

var masterArray = [[1, 2, 5, 6], [5, 13, 7, 8], [9, 11, 13, 15], [13, 14, 15, 16], [1, 9, 11, 12]], 
 
    compareArray = [9, 11, 13, 15, 1, 2, 5, 6], 
 
    result = masterArray.map(function (a) { 
 
     return a.filter(function (b) { 
 
      return compareArray.indexOf(b) !== -1; 
 
     }); 
 
    }); 
 

 
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

ES6

var masterArray = [[1, 2, 5, 6], [5, 13, 7, 8], [9, 11, 13, 15], [13, 14, 15, 16], [1, 9, 11, 12]], 
 
    compareArray = [9, 11, 13, 15, 1, 2, 5, 6], 
 
    result = masterArray.map(a => a.filter(b => compareArray.includes(b))); 
 

 
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

相關問題