Currently learning about Underscore.js. I am attempting to filter an array of objects by checking if all items in a tags
array exist in the object's tags
.
I am using Underscore's filter
and every
methods, like so:
/* data set */
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"]
}
];
/* filtering example for white */
var tags = ["color_family_white", "room_study"];
var results = _.filter(colors, function (color) {
return _.every(tags, function() { /* ??? what to do here */} );
});
All tags must be present on color.tags
, returning only color 001 in this case.
This demonstrates my goal: http://jsfiddle.net/tsargent/EtuS7/5/