2
這裏有一點新的Underscore.js。我試圖通過測試tags
數組中的所有項目是否存在於對象的tags
中來篩選對象數組。Underscore.js,測試整個陣列是否在另一個陣列中
所以這樣的事情,使用下劃線的filter
和every
方法:
/* the set of data */
var colors = [
{
"id": "001",
"tags": ["color_family_white", "tone_warm", "room_study"]
},
{
"id": "002",
"tags": ["color_family_white", "tone_neutral"]
},
{
"id": "003",
"tags": ["color_family_red", "tone_bright", "room_kitchen"]
}
];
/* an example of white I might want to filter on */
var tags = ["color_family_white", "room_study"];
var results = _.filter(colors, function (color) {
return _.every(tags, function() { /* ??? what to do here */});
});
所有標籤應該出現在color.tags
。所以這應該只返回色001
這說明大概是我想要做的事:http://jsfiddle.net/tsargent/EtuS7/5/
驚人。謝謝! – Tyler