Filtrowanie tablicy z underscore.js
Próbuję filtrować niektóre obiekty w mojej próbie lepszego zrozumienia JS i używam underscore.js
Pochodzę z tła C # i jestem przyzwyczajony do LINQ, jednak podkreślenie nie jest takie samo.
Czy możesz pomóc mi odfiltrować tę tablicę w oparciu o zdefiniowany test, którego problemem jest właściwość tablicy w tablicy. TheWhere
operator jest różny od C #, którego normalnie używałbym do filtrowania elementów.
products = [
{ name: "Sonoma", ingredients: ["artichoke", "sundried tomatoes", "mushrooms"], containsNuts: false },
{ name: "Pizza Primavera", ingredients: ["roma", "sundried tomatoes", "goats cheese", "rosemary"], containsNuts: false },
{ name: "South Of The Border", ingredients: ["black beans", "jalapenos", "mushrooms"], containsNuts: false },
{ name: "Blue Moon", ingredients: ["blue cheese", "garlic", "walnuts"], containsNuts: true },
{ name: "Taste Of Athens", ingredients: ["spinach", "kalamata olives", "sesame seeds"], containsNuts: true }
];
it("given I'm allergic to nuts and hate mushrooms, it should find a pizza I can eat (functional)", function () {
var productsICanEat = [];
//This works but was hoping I could do the mushroom check as well in the same line
var noNuts = _(products).filter(function (x) { return !x.containsNuts;});
var noMushrooms = _(noNuts).reject(function(x){ return !_(x.ingredients).any(function(y){return y === "mushrooms";});});
console.log(noMushrooms);
var count = productsICanEat.length;
expect(productsICanEat.length).toBe(count);
});