¿Es posible que una matriz filtre otra matriz para que no coincida con cada carácter?
Tengo un conjunto de registros y un filtro que se ven así:
logs = [{id:1, log: "log1"}], {id:2, log: "log2"}, {id:3, log: "fail"} filter = ["log"]debería volver
[{id:1, log: "log1"}, {id:2, log: "log2"}]Si mi filtro fuera a ser
filter = ["1", "fai"]la salida seria
[{id:1, log: "log1"}, {id:3, log: "fail"]Puede usar la función Array.prototype.filter junto con la función Array.prototype.some para filtrar los objetos que no coinciden con el filtro.
const match = (filter, key, array) => array.filter(o => filter.some(c => o[key].includes(c))), array = [{id:1, log: "log1"}, {id:2, log: "log2"}, {id:3, log: "fail"}]; console.log(match(["log"], "log", array)); console.log(match(["1", "fai"], "log", array));Puedes hacer algo como lo siguiente:
const logs = [{id:1, log: "log1"}, {id:2, log: "log2"}, {id:3, log: "fail"}] const searches = ["1", "fai"] const matchingLogs = logs.filter(l => { return searches.some(term => l.log.includes(term)) })let logs = [{id:1, log: "log1"}, {id:2, log: "log2"}, {id:3, log: "fail"}]; let filter = ["1", "fai"]; /* * filter the array using the filter function. * Find any given string in the array of objects. * If you have a match, it will be added to the * array that will be returned */ let matches = logs.filter(function(object) { return !!filter.find(function(elem) { return -1 !== object.log.indexOf(elem); }); });