Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 返回其数组中包含字符串的所有数组_Javascript_Arrays_Object - Fatal编程技术网

Javascript 返回其数组中包含字符串的所有数组

Javascript 返回其数组中包含字符串的所有数组,javascript,arrays,object,Javascript,Arrays,Object,我试图过滤掉数组中包含字符串的对象。我把它放在一个对象数组中 我有这个: [ { articles: { id: "1", title: "Great food article", featured: false, paid: false, image: img1, author: "Chocolate Boy", date:"1-2-2020", description: "Street art edi

我试图过滤掉数组中包含字符串的对象。我把它放在一个对象数组中

我有这个:

[
{      
  articles: {
    id: "1",
    title: "Great food article",
    featured: false,
    paid: false,
    image: img1,
    author: "Chocolate Boy",
    date:"1-2-2020",
    description:
      "Street art edison bulb gluten-free, tofu try-hard brooklyn tattooed pickled chambray. Actually humblebrag next level, deep v art party wolf tofu direct trade readymade sustainable hell of banjo. Organic authentic subway tile cliche palo santo, street art XOXO dreamcatcher retro sriracha portland air plant kitsch stumptown. Austin small batch squid gastropub. Pabst pug tumblr gochujang offal retro cloud bread bushwick semiotics before they sold out sartorial literally mlkshk. Vaporware hashtag vice, sartorial before they sold out pok pok health goth trust fund cray.",
    tags: [
      "Tech",
      "Food"
    ]
  }
},
{      
    articles: {
      id: "2",
      title: "Good tech article",
      featured: false,
      paid: false,
      image: img2,
      author: "Candy Boy",
      date:"1-2-2020",
      description:
        "Street art edison bulb gluten-free, tofu try-hard brooklyn tattooed pickled chambray. Actually humblebrag next level, deep v art party wolf tofu direct trade readymade sustainable hell of banjo. Organic authentic subway tile cliche palo santo, street art XOXO dreamcatcher retro sriracha portland air plant kitsch stumptown. Austin small batch squid gastropub. Pabst pug tumblr gochujang offal retro cloud bread bushwick semiotics before they sold out sartorial literally mlkshk. Vaporware hashtag vice, sartorial before they sold out pok pok health goth trust fund cray.",
      tags: [
        "Tech",
        "Adventure"
      ]
    }
  },
  {      
    articles: {
      id: "3",
      title: "Nice adventure article",
      featured: false,
      paid: false,
      image: img3,
      author: "Lollypop Boy",
      date:"1-2-2020",
      description:
        "Street art edison bulb gluten-free, tofu try-hard brooklyn tattooed pickled chambray. Actually humblebrag next level, deep v art party wolf tofu direct trade readymade sustainable hell of banjo. Organic authentic subway tile cliche palo santo, street art XOXO dreamcatcher retro sriracha portland air plant kitsch stumptown. Austin small batch squid gastropub. Pabst pug tumblr gochujang offal retro cloud bread bushwick semiotics before they sold out sartorial literally mlkshk. Vaporware hashtag vice, sartorial before they sold out pok pok health goth trust fund cray.",
      tags: [
        "Tech",
        "Food",
        "Adventure"
      ]
    }
  },
]
如果我比较一个“food”字符串,我想返回在其“tags”数组中包含“food”的两个对象

其中,tempArticles包含所有3个对象,标记的值为food,但它只返回数组中所有3个标记的数组

应为1和3个对象

感谢解释方面的帮助,因为我正在尝试解决它一段时间。

您可以使用,然后检查字符串是否包含在
标记中

var数组=[
{      
文章:{
id:“1”,
标题:“美食佳品”,
特色:假,
支付:假,
图像:“”,
作者:“巧克力男孩”,
日期:“1-2-2020”,
说明:
街头艺术爱迪生灯泡无麸质,豆腐努力布鲁克林纹身腌制香肠。实际上是卑鄙的下一级,深v艺术派对狼豆腐直接贸易成衣可持续的地狱班卓琴。有机正宗地铁瓷砖帕洛桑托,街头艺术XOXO追梦者复古sriracha波特兰空气工厂庸俗小镇。奥斯汀小批量squid gastropub.Pabst pug tumblr gochujang内脏retro cloud bread bushwick符号学,在出售衣饰之前,字面上是mlkshk.Vaporware标签vice,衣饰,在出售pok pok health goth信托基金cray之前。”,
标签:[
“技术”,
“食物”
]
}
},
{      
文章:{
id:“2”,
标题:“好科技文章”,
特色:假,
支付:假,
图像:“”,
作者:“糖果男孩”,
日期:“1-2-2020”,
说明:
街头艺术爱迪生灯泡无麸质,豆腐努力布鲁克林纹身腌制香肠。实际上是卑鄙的下一级,深v艺术派对狼豆腐直接贸易成衣可持续的地狱班卓琴。有机正宗地铁瓷砖帕洛桑托,街头艺术XOXO追梦者复古sriracha波特兰空气工厂庸俗小镇。奥斯汀小批量squid gastropub.Pabst pug tumblr gochujang内脏retro cloud bread bushwick符号学,在出售衣饰之前,字面上是mlkshk.Vaporware标签vice,衣饰,在出售pok pok health goth信托基金cray之前。”,
标签:[
“技术”,
“冒险”
]
}
},
{      
文章:{
id:“3”,
标题:“不错的冒险文章”,
特色:假,
支付:假,
图像:“”,
作者:“棒棒糖男孩”,
日期:“1-2-2020”,
说明:
街头艺术爱迪生灯泡无麸质,豆腐努力布鲁克林纹身腌制香肠。实际上是卑鄙的下一级,深v艺术派对狼豆腐直接贸易成衣可持续的地狱班卓琴。有机正宗地铁瓷砖帕洛桑托,街头艺术XOXO追梦者复古sriracha波特兰空气工厂庸俗小镇。奥斯汀小批量squid gastropub.Pabst pug tumblr gochujang内脏retro cloud bread bushwick符号学,在出售衣饰之前,字面上是mlkshk.Vaporware标签vice,衣饰,在出售pok pok health goth信托基金cray之前。”,
标签:[
“技术”,
“食物”,
“冒险”
]
}
}
]
var foodArray=array.filter(value=>value.articles.tags.includes('Food');

控制台日志(foodArray)在这里,伙计,但是用变量替换
image
属性中的字符串

const数组=[
{      
文章:{
id:“1”,
标题:“美食佳品”,
特色:假,
支付:假,
图像:“图像”,
作者:“巧克力男孩”,
日期:“1-2-2020”,
说明:
街头艺术爱迪生灯泡无麸质,豆腐努力布鲁克林纹身腌制香肠。实际上是卑鄙的下一级,深v艺术派对狼豆腐直接贸易成衣可持续的地狱班卓琴。有机正宗地铁瓷砖帕洛桑托,街头艺术XOXO追梦者复古sriracha波特兰空气工厂庸俗小镇。奥斯汀小批量squid gastropub.Pabst pug tumblr gochujang内脏retro cloud bread bushwick符号学,在出售衣饰之前,字面上是mlkshk.Vaporware标签vice,衣饰,在出售pok pok health goth信托基金cray之前。”,
标签:[
“技术”,
“食物”
]
}
},
{      
文章:{
id:“2”,
标题:“好科技文章”,
特色:假,
支付:假,
图像:“iamge”,
作者:“糖果男孩”,
日期:“1-2-2020”,
说明:
街头艺术爱迪生灯泡无麸质,豆腐努力布鲁克林纹身腌制香肠。实际上是卑鄙的下一级,深v艺术派对狼豆腐直接贸易成衣可持续的地狱班卓琴。有机正宗地铁瓷砖帕洛桑托,街头艺术XOXO追梦者复古sriracha波特兰空气工厂庸俗小镇。奥斯汀小批量squid gastropub.Pabst pug tumblr gochujang内脏retro cloud bread bushwick符号学,在出售衣饰之前,字面上是mlkshk.Vaporware标签vice,衣饰,在出售pok pok health goth信托基金cray之前。”,
标签:[
“技术”,
“冒险”
]
}
},
{      
文章:{
id:“3”,
标题:“不错的冒险文章”,
特色:假,
支付:假,
图像:“图像”,
作者:“棒棒糖男孩”,
日期:“1-2-2020”,
说明:
街头艺术爱迪生灯泡无麸质,豆腐努力布鲁克林纹身腌制香肠。实际上是卑鄙的下一级,深v艺术派对狼豆腐直接贸易成衣可持续的地狱班卓琴。有机正宗地铁瓷砖帕洛桑托,街头艺术XOXO追梦者复古sriracha波特兰空气工厂庸俗小镇。奥斯汀小批量squid gastropub.Pabst pug tumblr gochujang内脏retro cloud bread bushwick符号学,在出售衣饰之前,字面上是mlkshk.Vaporware标签vice,衣饰,在出售pok pok health goth信托基金cray之前。”,
标签:[
filterArticles = () =>{
    let{
        articles, tags
    } = this.state

    let tempArticles = [...articles];
    console.log(tempArticles)
    //Making array of arrays of articles tags
    let array1 = [...new Set(tempArticles.map(item => item["tags"]))];
    //Making the array of arrays flat to one dimensional array
    let array2 = [...new Set(array1.flat(1))];
    console.log(array2)
    console.log(tags)

}
arr.filter(ob => {return ob.articles.tags.includes("Food")})
const newArr = arr.filter((item) => ( item.articles.tags.indexOf('Food') > -1 ));

console.log(newArr); // 2 items which have the Food tag