Node.js
Lodash
Sök…
Introduktion
Lodash är ett praktiskt JavaScript-bibliotek.
Filtrera en samling
Kodavsnittet nedan visar de olika sätten du kan filtrera på en rad objekt med lodash.
let lodash = require('lodash');
var countries = [
{"key": "DE", "name": "Deutschland", "active": false},
{"key": "ZA", "name": "South Africa", "active": true}
];
var filteredByFunction = lodash.filter(countries, function (country) {
return country.key === "DE";
});
// => [{"key": "DE", "name": "Deutschland"}];
var filteredByObjectProperties = lodash.filter(countries, { "key": "DE" });
// => [{"key": "DE", "name": "Deutschland"}];
var filteredByProperties = lodash.filter(countries, ["key", "ZA"]);
// => [{"key": "ZA", "name": "South Africa"}];
var filteredByProperty = lodash.filter(countries, "active");
// => [{"key": "ZA", "name": "South Africa"}];
Modified text is an extract of the original Stack Overflow Documentation
Licensierat under CC BY-SA 3.0
Inte anslutet till Stack Overflow