如何按多个参数筛选?

huangapple go评论102阅读模式
英文:

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) =&gt; 
  data.filter(entry =&gt; {
    for(const [key, value] of Object.entries(query)) {
      if(entry[key] !== value) return false
    }
    return true
  })

const base = [{
    _id: &#39;5h245jg2h4gjh2jh5g2j4hg52g&#39;,
    name: &#39;Jhon&#39;
  },
  {
    _id: &#39;fdgd6fgd7f84b5hhjfdghj4hgg&#39;,
    name: &#39;Jhon&#39;
  },
  {
    _id: &#39;fu4gosy5hjruierbweyuu5u6hh&#39;,
    name: &#39;Jhon&#39;
  },
  {
    _id: &#39;fu4gosy5hjruierbweyuu5u6hh&#39;,
    name: &#39;Jhon&#39;,
    entry: 2 // the &#39;find&#39; function only cares about the given options
  }
]

const options = {
  _id: &#39;fu4gosy5hjruierbweyuu5u6hh&#39;,
  name: &quot;Jhon&quot;
}
console.log(find(base, options))
console.log(find([
  { name: &quot;apple&quot;, category: &quot;fruit&quot; },
  { name: &quot;celery&quot;, category: &quot;vegetable&quot; },
  { name: &quot;mushroom&quot;, category: &quot;vegetable&quot; },
  { name: &quot;lemon&quot;, category: &quot;fruit&quot; },
  { name: &quot;watermelon&quot;, category: &quot;fruit&quot; },
], { category: &quot;fruit&quot; }))

<!-- 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 &gt; 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) =&gt; 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 &gt; value &amp;&amp; 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 &gt; 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) =&gt; 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 &gt; value &amp;&amp; element.toString().slice(0, 1) == start_value)
}

huangapple
  • 本文由 发表于 2023年2月24日 02:17:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548794.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定