使用includes()[duplicate]搜索标签

问题描述 投票:0回答:5

这个问题在这里已有答案:

所以我在一个使用标签作为搜索过滤器的应用程序中工作。但是,在使用两个标签时我无法得到结果。例如,一家名为CoffeShop的商店有两个标签'Coffee'和'Cookies'。当传递其中一个时它返回CoffeShop,但是当传递两个标签时它什么都不返回。

我尝试使用lodash和其他一些东西,但到目前为止都没有。

let array = [
  {
    "name": "CoffeShop",
    "tag":[
      "Coffee",
      "Cookies",
    ],
  },
   {
    "name": "TeaShop",
    "tag":[
      "Tea",
    ],
  },
   {
    "name": "IceCreamShop",
    "tag": [
      "Ice Cream",
    ]
  },
]



let tags = ['Coffee', 'Cookies']   
result = array.filter(item => item.tag.some(t =>  t.includes(tags)))
console.log(result)

实际的结果应该只给我一个商店,这是CoffeShop,但我什么都没得到

javascript
5个回答
2
投票

问题是Array.prototype.includes()会在你的标签中寻找一个数组,它不会自动检查数组中的所有值。您可以执行以下操作:

result = array.filter(item => tags.every(tag => item.tag.includes(tag)));

3
投票

刚反向包括应该是Array.include(item)

let array = [
  {
    "name": "CoffeShop",
    "tag":[
      "Coffee",
      "Cookies",
    ],
  },
   {
    "name": "TeaShop",
    "tag":[
      "Tea",
    ],
  },
   {
    "name": "IceCreamShop",
    "tag": [
      "Ice Cream",
    ]
  },
]



let tags = ['Coffee', 'Cookies']  

result = array.filter(item => item.tag.some(t =>  tags.includes(t)))

console.log(result)

3
投票

let array = [
  {
    "name": "CoffeShop",
    "tag":[
      "Coffee",
      "Cookies",
    ],
  },
   {
    "name": "TeaShop",
    "tag":[
      "Tea",
    ],
  },
   {
    "name": "IceCreamShop",
    "tag": [
      "Ice Cream",
    ]
  },
]
const _strip = str => str.replace(/\s/g, '')
const _minimize = arr => arr.map(t => _strip(t.toLowerCase()))

const getShopWithTags = (shops = [], tags = []) => {
  tags = _minimize(tags)
  return array.filter(({tag}) => tags.includes(..._minimize(tag)))
}

let tags = ['Coffee', 'Cookies']  
let result = getShopWithTags(array, tags)
console.log(result)

tags = ['Coffee', 'Cookies', 'Ice Cream']
result = getShopWithTags(array, tags)
console.log(result)

let array = [
  {
    "name": "CoffeShop",
    "tag":[
      "Coffee",
      "Cookies",
    ],
  },
   {
    "name": "TeaShop",
    "tag":[
      "Tea",
    ],
  },
   {
    "name": "IceCreamShop",
    "tag": [
      "Ice Cream",
    ]
  },
]

let tags = ['Coffee', 'Cookies']  
let result = array.filter(({tag}) => tags.includes(...tag))
console.log(result)

tags = ['Coffee', 'Cookies', 'Ice Cream']
result = array.filter(({tag}) => tags.includes(...tag))
console.log(result)

2
投票

你需要像tags.includes(t)那样做。在你的代码中,你只是颠倒了包含,它应该是Array.include(item)

let array = [
  {
    "name": "CoffeShop",
    "tag":[
      "Coffee",
      "Cookies",
    ],
  },
   {
    "name": "TeaShop",
    "tag":[
      "Tea",
    ],
  },
   {
    "name": "IceCreamShop",
    "tag": [
      "Ice Cream",
    ]
  },
]



let tags = ['Coffee', 'Cookies']   
result = array.filter(item => item.tag.some(t =>  tags.includes(t)))
console.log(result)

0
投票

假设您要查找包含所有标签的条目,您必须检查array[n].tags是否包含来自tags的所有项目:

var array = [
  { "name": "CoffeShop", "tag": ["Coffee", "Cookies"] },
  { "name": "TeaShop", "tag": ["Tea"] },
  { "name": "IceCreamShop", "tag": ["Ice Cream"] },
  { "name": "Foo", "tag": ["Coffee"] },
  { "name": "Bar", "tag": ["Cookies"] }
]

var tags = ["Coffee", "Cookies"]
var result = array.filter(function(item) {
  return item.tag.filter(function(tag) {
    return tags.includes(tag)
  }).length === tags.length;
});
console.log(result);
© www.soinside.com 2019 - 2024. All rights reserved.