Javascript如何过滤对象数组中包含的确切单词

时间:2018-12-10 10:36:31

标签: javascript

如何从包含一个对象的数组中过滤掉单词“ mango”?例如“芒果”

var myIteams= [
  {   
    title: "Title One",
    section: 1,
    tag: ["orange", "apple", "banana", "mango"]
  },
  {   
    title: "Title Two",
    section: 15,
    tag: ["mango"]
  },
  {   
    title: "Title Three",
    section: 21,
    tag: ["orange", "apple" ]
  },

]

5 个答案:

答案 0 :(得分:0)

只需使用Array.prototype.filter来过滤带有回调的数组。然后使用Array.prototype.includes匹配正确的项目。

var myItems = [
  {   
    title: "Title One",
    section: 1,
    tag: ["orange", "apple", "banana", "mango"]
  },
  {   
    title: "Title Two",
    section: 15,
    tag: ["mango"]
  },
  {   
    title: "Title Three",
    section: 21,
    tag: ["orange", "apple" ]
  },

];

const match = myItems.filter(item => {
  return item.tag.includes('mango');
});
console.log(match);

答案 1 :(得分:0)

您可以使用Array.filter。它需要一个函数,该函数应该返回“真”或“假”的东西。如果返回真实信息,则保留该项目,否则将其过滤掉。

if(carry == 0)
{
    vr.erase(std::previous(vr.end())
}
// ...

答案 2 :(得分:0)

您可以使用v[7] = -1012过滤数组,并使用var myIteams= [ { title: "Title One", section: 1, tag: ["orange", "apple", "banana", "mango"] }, { title: "Title Two", section: 15, tag: ["mango"] }, { title: "Title Three", section: 21, tag: ["orange", "apple" ] }, ] const result = myIteams.filter(item => item.tag.indexOf('mango') > -1) console.dir(result)查找包含所需值的标签列表。

filter

答案 3 :(得分:0)

您可以使用filterincludes来获取其中包含标签“ Mango ”的元素

  

过滤器:过滤器提供了新的数组,我们从现有数组中获取元素   有条件的数组。

     

includes:用于确定wheter数组是否包含某些   元素与否。它返回truefalse

var myIteams= [
  {   
    title: "Title One",
    section: 1,
    tag: ["orange", "apple", "banana", "mango"]
  },
  {   
    title: "Title Two",
    section: 15,
    tag: ["mango"]
  },
  {   
    title: "Title Three",
    section: 21,
    tag: ["orange", "apple" ]
  },

];

var result = myIteams.filter(x => x.tag.includes("mango"));
console.log(result);

答案 4 :(得分:0)

您可以将filter方法与include方法一起使用。这是创建函数的示例,该函数可让您传递要查找的项目。

var myItems = [
  {   
    title: "Title One",
    section: 1,
    tag: ["orange", "apple", "banana", "mango"]
  },
  {   
    title: "Title Two",
    section: 15,
    tag: ["mango"]
  },
  {   
    title: "Title Three",
    section: 21,
    tag: ["orange", "apple" ]
  },
];

function findItem(itemToFind) {
  return myItems.filter(item => item.tag.includes(itemToFind));
}

console.log(findItem('mango'))