5

我無法弄清楚如何找到這個陣列集的交集:如何找到包含使用Javascript/underscorejs的對象的數組的數組的交集?

[ 
[ 
    {"name":"product1","light":"1"}, 
    {"name":"product2","light":"2"}, 
    {"name":"product5","light":"5"}, 
    {"name":"product4","light":"4"} 
], 
[ 
    {"name":"product2","light":"2"}, 
    {"name":"product3","light":"3"}, 
    {"name":"product4","light":"4"} 
],[...more arrays with objects] 
] 

這只是樣本數據,真正集我改變了很多,但與結構。我想返回的路口,看起來像這樣(交叉的對象的單個陣列):

_.intersectionObjects = _.intersect = function(array) { 
var slice = Array.prototype.slice; // added this line as a utility 
var rest = slice.call(arguments, 1); 
return _.filter(_.uniq(array), function(item) { 
    return _.every(rest, function(other) { 
    //return _.indexOf(other, item) >= 0; 
    return _.any(other, function(element) { return _.isEqual(element, item); }); 
    }); 
}); 
}; 

我需要這個,因爲我想:

[ 
{"name":"product2","light":"2"}, 
{"name":"product4","light":"4"}, 
] 

我LoDashjs和Underscorejs一起嘗試這種使用knockoutjs創建一個標籤系統。我有一個分類標記按鈕的佈局,在點擊時寫入「過濾器」可觀察數組,唯一剩下的就是找到包含在這個可觀察數組中的過濾產品的交集。

請幫助我,我一直試圖解決這個問題,連續兩天,但缺乏JavaScript知識來弄清楚。提前致謝!

回答

6

嘗試加入他們申請方法:

var myArr = [ 
    [ 
     {"name":"product1","light":"1"}, 
     {"name":"product2","light":"2"}, 
     {"name":"product5","light":"5"}, 
     {"name":"product4","light":"4"} 
    ], 
    [ 
     {"name":"product2","light":"2"}, 
     {"name":"product3","light":"3"}, 
     {"name":"product4","light":"4"} 
    ] 
    ] 

    _.intersectionObjects = _.intersect = function(array) { 
    var slice = Array.prototype.slice; 
    var rest = slice.call(arguments, 1); 
    return _.filter(_.uniq(array), function(item) { 
     return _.every(rest, function(other) { 
     return _.any(other, function(element) { 
      return _.isEqual(element, item); 
     }); 
     }); 
    }); 
    }; 

    var myIntersection = _.intersectionObjects.apply(_, myArr); 

    for (var i = 0; i < myIntersection.length; i++) { 
    console.log(myIntersection[i]); 
    } 

    // Sample Output: 
    // Object {name: "product2", light: "2"} 
    // Object {name: "product4", light: "4"} 
+0

謝謝!!!!!!!!適用於什麼? – Marz

+0

應用:https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function/apply – WiredPrairie

1

你很可能如果你只是比較對象本身橫跨錯誤運行,因爲這將返回false:

var o1 = {"foo":"bar"}; 
var o2 = {"foo":"bar"}; 
return o1 == o2; 

你需要比較對象內部的值,並根據這些值相交:

JSFiddle在這裏執行您喜歡的操作。 http://jsfiddle.net/turiyag/bWrQW/6/

function valueIntersect(a1, a2) { 
    var aReturn = []; 
    for(i1 in a1) { 
     o1 = a1[i1]; 
     for (i2 in a2) { 
      o2 = a2[i2]; 
      if(equals(o1,o2)) { 
       aReturn.push(o1); 
       break; 
      } 
     } 
    } 
    return aReturn; 
} 

function equals(o1, o2) { 
    if (!o2 || o1.length != o2.length) { 
     return false; 
    } 
    for (i in o1) { 
     if (o1[i] !== o2[i]) { 
      return false; 
     } 
    } 
    return true; 
}; 
2

下面是我用似乎運作良好的方法。

var arr1 = [{"id":"1"},{"id":"2"},{"id":"3"}]; 
 
var arr2 = [{"id":"1"},{"id":"3"}]; 
 

 
function match(item){ 
 
var isMatch = _.matcher(item); 
 
var matches = _.filter(arr2, isMatch); 
 
    return matches[0]; 
 
} 
 

 
var matchArray = _.compact(_.map(arr1, function(val){ return match(val)})); 
 

 
document.write(JSON.stringify(matchArray));
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

相關問題