简体   繁体   English

如何遍历对象数组并使用lodash检查值是否匹配对象中的字符串或项目数组中的值

[英]How to loop through array of objects and check if a value matches either a string in the object or a value in an array of items using lodash

I have an array of objects that looks something like this: 我有一个看起来像这样的对象数组:

    [{
    "title": "first footer",
    "section": "structure",
    "categoryId": "footer",
    "image": "http://placehold.it/350x220",
    "hashtags": ["#popin", "#wobble"],
    "uId": "footerA"
  },{
    "title": "second footer",
    "section": "structure",
    "categoryId": "footer",
    "image": "http://placehold.it/350x220",
    "hashtags": ["#popin", "#wibble"],
    "uId": "footerB"
  },
  {
    "title": "first header",
    "section": "structure",
    "categoryId": "header",
    "image": "http://placehold.it/350x220",
    "hashtags": ["#popin", "#wibble"],
    "uId": "headerA"
  },
  {
    "title": "second header",
    "section": "structure",
    "categoryId": "header",
    "image": "http://placehold.it/350x220",
    "hashtags": ["#popin", "#wibble"],
    "uId": "headerB"
  },
  {
    "title": "first landing",
    "section": "structure",
    "categoryId": "landing",
    "image": "http://placehold.it/350x220",
    "hashtags": ["#popin", "#wibble"],
    "uId": "landingA"
  },
  {
    "title": "second landing",
    "section": "structure",
    "categoryId": "landing",
    "image": "http://placehold.it/350x220",
    "hashtags": ["#popin", "#wibble"],
    "uId": "landingB"
  },
  {
    "title": "third landing",
    "section": "structure",
    "categoryId": "landing",
    "image": "http://placehold.it/350x220",
    "hashtags": ["#popin", "#wibble"],
    "uId": "landingC"
  },
  {
    "title": "first nav",
    "section": "structure",
    "categoryId": "navigation",
    "image": "http://placehold.it/350x220",
    "hashtags": ["#popin", "#wobble"],
    "uId": "navA"
  },{
    "title": "first blog",
    "section": "components",
    "categoryId": "blog",
    "image": "http://placehold.it/350x220",
    "hashtags": ["#popin", "#wibble"],
    "uId": "blogA"
  },
  {
    "title": "second blog sdf wicked",
    "section": "components",
    "categoryId": "blog",
    "image": "http://placehold.it/350x220",
    "hashtags": ["#popin", "#wibble"],
    "uId": "blogB"
  },
  {
    "title": "first header",
    "section": "components",
    "categoryId": "contact_button",
    "image": "http://placehold.it/350x220",
    "hashtags": ["#popin", "#wibble"],
    "uId": "contact_buttonA"
  },
  {
    "title": "first landing",
    "section": "components",
    "categoryId": "content_bloc",
    "image": "http://placehold.it/350x220",
    "hashtags": ["#popin", "#wibble"],
    "uId": "content_blocA"
  },
  {
    "title": "second landing",
    "section": "components",
    "categoryId": "content_bloc",
    "image": "http://placehold.it/350x220",
    "hashtags": ["#popin", "#wibble"],
    "uId": "content_blocB"
  },
  {
    "title": "third landing",
    "section": "components",
    "categoryId": "content_bloc",
    "image": "http://placehold.it/350x220",
    "hashtags": ["#popin", "#wibble"],
    "uId": "content_blocC"
  },
  {
    "title": "first nav",
    "section": "components",
    "categoryId": "cover",
    "image": "http://placehold.it/350x220",
    "hashtags": ["#popin", "#wibble"],
    "uId": "coverA"
  }]

When I click on a button I want to be able to filter the objects in the array to return only matched objects that someone has searched for. 当我单击按钮时,我希望能够过滤数组中的对象,以仅返回某人已搜索的匹配对象。 For example if someone has typed 'land' the function should check if 'land' exists in the title of the component or if it exists in the hashtags array of that component. 例如,如果有人键入了“ land”,则该函数应检查组件的标题中是否存在“ land”或该组件的hashtags数组中是否存在“ land”。

I am using lodash to do this so would like to keep using this. 我正在使用lodash来做到这一点,所以想继续使用它。

I have been able to do the test for the title but stuck on how to loop through the hashtags array as well when looking at the single component in the loop. 我已经能够对标题进行测试,但是在查看循环中的单个组件时仍然停留在如何遍历hashtags数组的问题上。

This is my code so far: 到目前为止,这是我的代码:

_.filter(this.components, function (component) {
  //Check if components title has 'wic' in the text or if 'wic' exists in the hashtags array
  if(_.includes(component.title, 'wic')) {
    console.log(component);
  }

});

Return a Boolean from Array.prototype.filter() . Array.prototype.filter()返回一个Boolean You can use Array.prototype.some() , which also expects a Boolean return value, to check hashtags array. 您可以使用Array.prototype.some() (它也需要一个Boolean值)来检查hashtags数组。

var match = "land";
var re = new RegExp(match);
var res = this.components.filter(function(component) {
  return re.test(component.title) 
         || component.hashtags.some(function(tag) {
              return re.test(tag)
            })
});

using lodash _.filter() , _.some() 使用lodash _.filter()_.some()

var match = "land";
var re = new RegExp(match);
var res = _.filter(this.components, function(component) {
console.log(component.title,  re.test(component.title) )
  return re.test(component.title) 
         || _.some(component.hashtags, function(tag) {
              return re.test(tag)
            })
});

jsfiddle https://jsfiddle.net/o8ervt0x/ jsfiddle https://jsfiddle.net/o8ervt0x/

it will give the object which has land word in their title 它会给它有对象land在他们的标题字

 savedViews= [{ "title": "first footer", "section": "structure", "categoryId": "footer", "image": "http://placehold.it/350x220", "hashtags": ["#popin", "#wobble"], "uId": "footerA" },{ "title": "second footer", "section": "structure", "categoryId": "footer", "image": "http://placehold.it/350x220", "hashtags": ["#popin", "#wibble"], "uId": "footerB" }, { "title": "first header", "section": "structure", "categoryId": "header", "image": "http://placehold.it/350x220", "hashtags": ["#popin", "#wibble"], "uId": "headerA" }, { "title": "second header", "section": "structure", "categoryId": "header", "image": "http://placehold.it/350x220", "hashtags": ["#popin", "#wibble"], "uId": "headerB" }, { "title": "first landing", "section": "structure", "categoryId": "landing", "image": "http://placehold.it/350x220", "hashtags": ["#popin", "#wibble"], "uId": "landingA" }, { "title": "second landing", "section": "structure", "categoryId": "landing", "image": "http://placehold.it/350x220", "hashtags": ["#popin", "#wibble"], "uId": "landingB" }, { "title": "third landing", "section": "structure", "categoryId": "landing", "image": "http://placehold.it/350x220", "hashtags": ["#popin", "#wibble"], "uId": "landingC" }, { "title": "first nav", "section": "structure", "categoryId": "navigation", "image": "http://placehold.it/350x220", "hashtags": ["#popin", "#wobble"], "uId": "navA" },{ "title": "first blog", "section": "components", "categoryId": "blog", "image": "http://placehold.it/350x220", "hashtags": ["#popin", "#wibble"], "uId": "blogA" }, { "title": "second blog sdf wicked", "section": "components", "categoryId": "blog", "image": "http://placehold.it/350x220", "hashtags": ["#popin", "#wibble"], "uId": "blogB" }, { "title": "first header", "section": "components", "categoryId": "contact_button", "image": "http://placehold.it/350x220", "hashtags": ["#popin", "#wibble"], "uId": "contact_buttonA" }, { "title": "first landing", "section": "components", "categoryId": "content_bloc", "image": "http://placehold.it/350x220", "hashtags": ["#popin", "#wibble"], "uId": "content_blocA" }, { "title": "second landing", "section": "components", "categoryId": "content_bloc", "image": "http://placehold.it/350x220", "hashtags": ["#popin", "#wibble"], "uId": "content_blocB" }, { "title": "third landing", "section": "components", "categoryId": "content_bloc", "image": "http://placehold.it/350x220", "hashtags": ["#popn", "#wibble"], "uId": "content_blocC" }, { "title": "first nav", "section": "components", "categoryId": "cover", "image": "http://placehold.it/350x220", "hashtags": ["#popn", "#wibble"], "uId": "coverA" }] var view = 'land'; var re = new RegExp(view); var delete_id = savedViews.filter(function (el) { return re.test(el.title); }); //console.log(delete_id); var view1 = 'popin'; var re1 = new RegExp(view1); var delete_id1 = delete_id.filter(function (el) { return re1.test(el.hashtags); }); console.log(delete_id1); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何遍历对象数组并使用lodash检查该对象中的值是否与另一个数组中的项匹配 - how to loop through array of objects and check if a value in that object matches an item in another array using lodash 如何基于其他数组遍历数组中的每个值并使用 javascript 检查它是否符合某些条件? - How to loop through each value in array based on other array and check if it matches some condition using javascript? 如何使用lodash在数组中查找具有值的对象 - How to find object with value in array using lodash 检查lodash中的对象数组的属性值 - Check array of objects in lodash for property value 在Lodash中遍历对象数组时如何添加新的键值对 - How to add new key value pair when loop through array of objects in lodash LoDash从子数组中找到其属性与对象数组中的值匹配的对象 - LoDash find the object whose property matches the value from array of objects with children array JS使用lodash根据对象值对上级对象数组进行排序 - JS Sorting superior array of objects depending on object value using lodash 使用 lodash 检查对象是否包含数组中的值 - Check if object contains value from array using lodash 遍历名称数组,并检查keypress的值是否与名称中的字符匹配 - Loop through an array of names and check if value of keypress matches a character in a name 如何检查对象的值是否与 Angular 中数组列表中的任何值匹配 - How to Check If a Value of Objects Matches Any Value in a List of Array in Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM