sortby remove from desc array javascript filter filtering lodash

javascript - desc - lodash remove from array



Filtrar una matriz de objetos con lodash basado en el valor de la propiedad (6)

Tenemos una variedad de objetos como tales

var myArr = [ {name: "john", age:23} {name: "john", age:43} {name: "jim", age:101} {name: "bob", age:67} ];

¿cómo obtengo la lista de objetos de myArr donde el nombre es john con lodash?


Con lodash :

const myArr = [ {name: "john", age:23}, {name: "john", age:43}, {name: "jim", age:101}, {name: "bob", age:67} ]; const johnArr = _.filter(myArr, person => person.name === ''john''); console.log(johnArr)

JavaScript Vanilla:

const myArr = [ {name: "john", age:23}, {name: "john", age:43}, {name: "jim", age:101}, {name: "bob", age:67} ]; const johnArr = myArr.filter(person => person.name === ''john''); console.log(johnArr);


Lodash tiene una función de "mapa" que funciona como jQuerys:

var myArr = [{ name: "john", age:23 }, { name: "john", age:43 }, { name: "jimi", age:10 }, { name: "bobi", age:67 }]; var johns = _.map(myArr, function(o) { if (o.name == "john") return o; }); // Remove undefines from the array johns = _.without(johns, undefined)


Use el método lodash _.filter :

_.filter(collection, [predicate=_.identity])

Itera sobre los elementos de la colección, devolviendo una matriz de todos los elementos por los que predicate devuelve truthy. El predicado se invoca con tres argumentos: (valor, índice | clave, colección).

con predicado como función personalizada

_.filter(myArr, function(o) { return o.name == ''john''; });

con predicado como parte del objeto filtrado (la taquigrafía _.matches _.matches)

_.filter(myArr, {name: ''john''});

con predicado como matriz [clave, valor] (la taquigrafía _.matchesProperty _.matchesProperty).

_.filter(myArr, [''name'', ''John'']);


lodash también tiene un método de eliminación

var myArr = [ {name: "john", age:23} {name: "john", age:43} {name: "jim", age:101} {name: "bob", age:67} ]; var onlyJohn = myArr.remove( person => { return person.name == "john" })


let myArr = [ {name: "john", age:23}, {name: "john", age:43}, {name: "jim", age:101}, {name: "bob", age:67}, ]; // this will return old object (myArr) with items named ''john'' let list = _.filter(myArr, item => item.name === ''jhon''); // this will return new object referenc (new Object) with items named ''john'' let list = _.map(myArr, item => item.name === ''jhon'').filter(item => item.name);


let myArr = [ {name: "john", age:23}, {name: "john", age:43}, {name: "jim", age:101}, {name: "bob", age:67}, ]; let list = _.filter(myArr, item => item.name === "john");