Filter array of objects with another array of objects
var filtered = [];
for(var arr in myArray){
for(var filter in myFilter){
if(myArray[arr].userid == myFilter[filter].userid && myArray[arr].projectid == myFilter[filter].projectid){
filtered.push(myArray[arr].userid);
}
}
}
console.log(filtered);
Filter an array of objects by another object of filters
You can achieve this result using filter, Object.keys, and every.
You have to use filter
and pass predicate that tell whether it is included in the final result.
In predicate, loop over all properties on the filters
object and match if it is present in data
or not. Simple
data.filter((o) =>Object.keys(filters).every((k) => filters[k] === o[k]));
const data = [{
level: "1",
objectId: "11",
objectIdNo: "320",
wpId: "123",
},
{
level: "2",
objectId: "12",
objectIdNo: "321",
wpId: "123",
},
{
level: "2",
objectId: "13",
objectIdNo: "322",
wpId: "120",
},
];
const filters = {
level: "2",
wpId: "123",
};
const result = data.filter((o) =>
Object.keys(filters).every((k) => filters[k] === o[k])
);
console.log(result);
Filter Array with objects based on another array dynamically
This should work
let codes = [{
"title": "lore",
"city": "New York",
"desc": "lorem lorem",
"age": "20"
},
{
"title": "lore2 ",
"city": "Santa Monica",
"desc": "lorem2",
"age": "20"
}
];
let filter = [{
"city": "New York"
}, {
"age": "20"
}];
let res = codes.filter(code => {
return filter.every(f => {
const [key, value] = Object.entries(f)[0];
return code[key] == value
});
});
console.log(res)
How filter array of objects by another array of objects
You could get the entries of the condensed object and use Array#every
and check the properties with the values.
var dataset = [{ id: "4", someval1: "10", someval2: "20", someval3: "30", someval4: "40" }, { id: "10", someval1: "10", someval2: "20", someval3: "30", someval4: "40" }, { id: "22", someval1: "102", someval2: "202", someval3: "302", someval4: "40" }], criterias = [{ someval1: "10" }, { someval3: "30" }, { someval4: "40" }], filters = Object.entries(Object.assign({}, ...criterias)), result = dataset.filter(o => filters.every(([k, v]) => o[k] === v));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
filter array of objects by another array of objects
Like Felix mentioned, Array#filter
won't work faster than native for loop, however if you really want it as functional way, here's one possible solution:
const array = [ { id: 1, name: 'a1', sub: { id: 6, name: 'a1 sub' } }, { id: 2, name: 'a2', sub: null }, { id: 3, name: 'a3', sub: { id: 8, name: 'a3 sub' } }, { id: 4, name: 'a4', sub: null }, { id: 5, name: 'a5', sub: { id: 10, name: 'a5 sub' } },];
const anotherArray = [ { id: 1, name: 'a1', sub: { id: 6, name: 'a1 sub' } }, { id: 2, name: 'a2', sub: null }, { id: 5, name: 'a5', sub: { id: 10, name: 'a5 sub' } },];
const r = array.filter((elem) => !anotherArray.find(({ id }) => elem.id === id) && elem.sub);
console.log(r);
How to filter an array from all elements of another array
You can use the this
parameter of the filter()
function to avoid to store your filter array in a global variable.
var filtered = [1, 2, 3, 4].filter( function(e) { return this.indexOf(e) < 0; }, [2, 4]);console.log(filtered);
Related Topics
What's the Best Way to Make a D3.Js Visualisation Layout Responsive
React Useeffect in Depth/Use of Useeffect
Parsing "Relaxed" JSON Without Eval
Accessing Redux State in an Action Creator
Finding Matches Between Multiple JavaScript Arrays
Sharing a Variable Between Multiple HTML Pages
How to Export HTML Table to Excel Using JavaScript
Javascript: Extract Video Frames Reliably
Detect Rotation of Android Phone in the Browser with JavaScript
Implementing Pagination in Mongodb
Settimeout and "This" in JavaScript
How to Programmatically Click a Link with JavaScript
Web Workers Without a Separate JavaScript File
Are All JavaScript Callbacks Asynchronous? If Not, How to Know Which Are