cómo fluye la lógica en: (libros) => (estantería) => ...
const shelf1 = [ { name: "name1", shelf: "a" }, { name: "name2", shelf: "a" }, ]; const shelf2 = [ { name: "name3", shelf: "b" }, { name: "name4", shelf: "b" }, ]; const allBooks = [...shelf1, ...shelf2]; const filter = (books) => (shelf) => books.filter((b) => b.shelf === shelf); const filterBy = filter(allBooks); const booksOnShelf = filterBy("b");necesito un equivalente más detallado a esta expresión abreviada, para ayudarme a digerir esa magia
Es una función que acepta un argumento de books y devuelve una nueva función que acepta un argumento de shelf . Esa función se asigna a filterBy y el resultado de llamar a esa función (una matriz) se asigna a booksOnShelf .
La función interna mantiene una referencia a los books cuando se devuelve, y generalmente se denomina cierre .
const shelf1=[{name:"name1",shelf:"a"},{name:"name2",shelf:"a"}],shelf2=[{name:"name3",shelf:"b"},{name:"name4",shelf:"b"}]; const allBooks = [...shelf1, ...shelf2]; function filter(books) { return function (shelf) { return books.filter(function (b) { return b.shelf === shelf; }); }; } // `filter` returns a new function which // is assigned to `filterBy`. That function accepts // a `shelf` argument const filterBy = filter(allBooks); // The result of calling that new function with // argument 'b' is assigned to `booksOnShelf` const booksOnShelf = filterBy('b'); console.log(booksOnShelf);