英文:
How to filter by several parameters?
问题
我希望用户能够按多个参数筛选。
我有一个带有筛选参数的对象:
const options = { _id: 'fu4gosy5hjruierbweyuu5u6hh', name: "Jhon" }
和一个包含用户数据库的对象数组:
const base = [
{ _id: '5h245jg2h4gjh2jh5g2j4hg52g', name: 'Jhon' },
{ _id: 'fdgd6fgd7f84b5hhjfdghj4hgg', name: 'Jhon' },
{ _id: 'fu4gosy5hjruierbweyuu5u6hh', name: 'Jhon' }
]
当然,你可以简化一切,只筛选唯一的ID,但你必须理解筛选必须基于多个参数,而这些参数可以完全不同。
我尝试在filter()中使用every()。
英文:
I want the user base to be able to filter by several parameters.
I have an object with parameters to filter:
const options = { _id: 'fu4gosy5hjruierbweyuu5u6hh', name: "Jhon" }
and an array of objects with a database of users:
const base = [
{ _id: '5h245jg2h4gjh2jh5g2j4hg52g', name: 'Jhon' },
{ _id: 'fdgd6fgd7f84b5hhjfdghj4hgg', name: 'Jhon' },
{ _id: 'fu4gosy5hjruierbweyuu5u6hh', name: 'Jhon' }
]
Of course you could make everything much simpler, namely to filter by unique id, but you have to understand that filtering has to be by several parameters and parameters can be completely different.
I tried using every() in filter().
答案1
得分: 2
在你的过滤函数中,你可以循环遍历所有对象属性:
const find = (data, query) =>
data.filter(entry => {
for(const [key, value] of Object.entries(query)) {
if(entry[key] !== value) return false
}
return true
})
const base = [{
_id: '5h245jg2h4gjh2jh5g2j4hg52g',
name: 'Jhon'
},
{
_id: 'fdgd6fgd7f84b5hhjfdghj4hgg',
name: 'Jhon'
},
{
_id: 'fu4gosy5hjruierbweyuu5u6hh',
name: 'Jhon'
},
{
_id: 'fu4gosy5hjruierbweyuu5u6hh',
name: 'Jhon',
entry: 2 // 'find' 函数只关心给定的选项
}
]
const options = {
_id: 'fu4gosy5hjruierbweyuu5u6hh',
name: "Jhon"
}
console.log(find(base, options))
console.log(find([
{ name: "apple", category: "fruit" },
{ name: "celery", category: "vegetable" },
{ name: "mushroom", category: "vegetable" },
{ name: "lemon", category: "fruit" },
{ name: "watermelon", category: "fruit" },
], { category: "fruit" }))
(注意:这段代码是用于演示目的的,不包括在实际代码中运行的HTML标记。)
英文:
In your filter function you can loop through all the object properties:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const find = (data, query) =>
data.filter(entry => {
for(const [key, value] of Object.entries(query)) {
if(entry[key] !== value) return false
}
return true
})
const base = [{
_id: '5h245jg2h4gjh2jh5g2j4hg52g',
name: 'Jhon'
},
{
_id: 'fdgd6fgd7f84b5hhjfdghj4hgg',
name: 'Jhon'
},
{
_id: 'fu4gosy5hjruierbweyuu5u6hh',
name: 'Jhon'
},
{
_id: 'fu4gosy5hjruierbweyuu5u6hh',
name: 'Jhon',
entry: 2 // the 'find' function only cares about the given options
}
]
const options = {
_id: 'fu4gosy5hjruierbweyuu5u6hh',
name: "Jhon"
}
console.log(find(base, options))
console.log(find([
{ name: "apple", category: "fruit" },
{ name: "celery", category: "vegetable" },
{ name: "mushroom", category: "vegetable" },
{ name: "lemon", category: "fruit" },
{ name: "watermelon", category: "fruit" },
], { category: "fruit" }))
<!-- end snippet -->
答案2
得分: 0
The filter function can be given greater versatility by being used in conjunction with a lambda. I'm not familiar with filter, but it seems filter places every iterated object into the function provided and breaks if you straightforwardly try to add more parameters to said function.
So this
function is_bigger_than(element, value) {
return element > value
}
filter(is_bigger_than(10))
wouldn't work because the function provided executes before an object can even be placed into it for evaluation. That means a lambda, which won't run so soon, is needed.
It would look like this
filter((automatically_inserted_element) => is_bigger_than(element, value))
you can just then tweak with is_bigger_than
to accept more parameters and whether it works on a "both" or "or" basis.
function is_bigger_than(element, value, start_value) {
return (element > value && element.toString().slice(0, 1) == start_value)
}
英文:
The filter function can be given greater versatility by being used in conjunction with a lambda. I'm not familiar with filter, but it seems filter places every iterated object into the function provided and breaks if you straightfowardly try to add more parameters to said function.
So this
function is_bigger_than(element, value) {
return element > value
}
filter(is_bigger_than(10))
wouldn't work because the function provided executes before an object can even be placed into it for evaluation. That means a lambda, which won't run so soon, is needed.
It would look like this
filter((automatically_inserted_element) => is_bigger_than(element, value))
you can just then tweak with is_bigger_than
to accept more parameters and whether it works on a "both" or "or" basis.
function is_bigger_than(element, value, start_value) {
return (element > value && element.toString().slice(0, 1) == start_value)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论