【问题标题】:Javascript how to filter object array contains exact word [duplicate]Javascript如何过滤对象数组包含确切的单词[重复]
【发布时间】:2019-05-11 05:56:21
【问题描述】:

请问如何从包含的对象中过滤数组中的单词“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" ]
  },

]

【问题讨论】:

  • 你的预期输出是什么?
  • 你有没有尝试过?出了什么问题?

标签: javascript


【解决方案1】:

只需使用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);

【讨论】:

  • 完美。谢谢!!
【解决方案2】:

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

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)

【讨论】:

    【解决方案3】:

    您可以使用filter 过滤数组并使用some 查找标签列表,其中包含您想要的值。

    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 filtered = myItems.filter(o => o.tag.some(t => t === 'mango'));
    console.log(filtered);

    【讨论】:

      【解决方案4】:

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

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

      includes:用于判断数组是否包含某些 元素与否。它返回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);

      【讨论】:

        【解决方案5】:

        您可以使用包含方法的过滤器方法。下面是一个创建函数的示例,该函数可让您传入要查找的项目。

        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'))
        

        【讨论】:

          猜你喜欢
          • 2022-11-28
          • 2018-08-07
          • 2020-02-18
          • 2019-06-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-07-30
          • 1970-01-01
          相关资源
          最近更新 更多