简体   繁体   English

如何遍历对象数组并使用lodash检查该对象中的值是否与另一个数组中的项匹配

[英]how to loop through array of objects and check if a value in that object matches an item in another array using lodash

I have an array of items which are featured components like this: 我有一系列的项目,这些项目是类似这样的特色组件:

var featuredIds = ['footerB', 'headerA', 'landingA'];

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

[{
    "title": "first footer",
    "section": "structure",
    "categoryId": "footer",
    "uId": "footerA"
  },{
    "title": "second footer",
    "section": "structure",
    "categoryId": "footer",
    "uId": "footerB"
  },
  {
    "title": "first header",
    "section": "structure",
    "categoryId": "header",
    "uId": "headerA"
  },
  {
    "title": "first header",
    "section": "structure",
    "categoryId": "header",
    "uId": "headerB"
  },
  {
    "title": "first landing",
    "section": "structure",
    "categoryId": "landing",
    "uId": "landingA"
  },
  {
    "title": "second landing",
    "section": "structure",
    "categoryId": "landing",
    "uId": "landingB"
  },
  {
    "title": "third landing",
    "section": "structure",
    "categoryId": "landing",
    "uId": "landingC"
  },
  {
    "title": "first nav",
    "section": "structure",
    "categoryId": "navigation",
    "uId": "navA"
  },{
    "title": "first footer",
    "section": "components",
    "categoryId": "blog",
      "uId": "blogA"
  },
  {
    "title": "second footer",
    "section": "components",
    "categoryId": "blog",
    "uId": "blogB"
  },
  {
    "title": "first header",
    "section": "components",
    "categoryId": "contact_button",
    "uId": "contact_buttonA"
  },
  {
    "title": "first landing",
    "section": "components",
    "categoryId": "content_bloc",
    "uId": "content_blocA"
  },
  {
    "title": "second landing",
    "section": "components",
    "categoryId": "content_bloc",
    "uId": "content_blocB"
  },
  {
    "title": "third landing",
    "section": "components",
    "categoryId": "content_bloc",
    "uId": "content_blocC"
  },
  {
    "title": "first nav",
    "section": "components",
    "categoryId": "cover",
    "uId": "coverA"
  }]

I want to create a new array which only holds the components that match the featureIds I provide in the array of featureIds which would look like this: 我想创建一个仅包含与我在featureIds数组中提供的featureIds匹配的组件的新数组,如下所示:

[{
    "title": "second footer",
    "section": "structure",
    "categoryId": "footer",
    "uId": "footerB"
  },{
    "title": "first header",
    "section": "structure",
    "categoryId": "header",
    "uId": "headerA"
  },
  {
    "title": "first landing",
    "section": "structure",
    "categoryId": "landing",
    "uId": "landingA"
  }]

I have looked at using _.some, _.find and a few others but haven't been able to get the result I am looking for. 我已经研究过使用_.some,_。find和其他一些方法,但是无法获得我想要的结果。 I have written this already using a double for loop which is why I want to us lodash to cut that out/learn something new. 我已经使用double for循环编写了此文件,这就是为什么我希望我们破破烂烂地切出/学习一些新知识。

You can use lodash's chain with _.keyBy() and _.at() : 您可以将lodash的链与_.keyBy()_.at()

 function filterBy(arr, filters) { return _(features) .keyBy('uId') .at(filters) .value(); } var features = [{ "title": "first footer", "section": "structure", "categoryId": "footer", "uId": "footerA" }, { "title": "second footer", "section": "structure", "categoryId": "footer", "uId": "footerB" }, { "title": "first header", "section": "structure", "categoryId": "header", "uId": "headerA" }, { "title": "first header", "section": "structure", "categoryId": "header", "uId": "headerB" }, { "title": "first landing", "section": "structure", "categoryId": "landing", "uId": "landingA" }, { "title": "second landing", "section": "structure", "categoryId": "landing", "uId": "landingB" }, { "title": "third landing", "section": "structure", "categoryId": "landing", "uId": "landingC" }, { "title": "first nav", "section": "structure", "categoryId": "navigation", "uId": "navA" }, { "title": "first footer", "section": "components", "categoryId": "blog", "uId": "blogA" }, { "title": "second footer", "section": "components", "categoryId": "blog", "uId": "blogB" }, { "title": "first header", "section": "components", "categoryId": "contact_button", "uId": "contact_buttonA" }, { "title": "first landing", "section": "components", "categoryId": "content_bloc", "uId": "content_blocA" }, { "title": "second landing", "section": "components", "categoryId": "content_bloc", "uId": "content_blocB" }, { "title": "third landing", "section": "components", "categoryId": "content_bloc", "uId": "content_blocC" }, { "title": "first nav", "section": "components", "categoryId": "cover", "uId": "coverA" }]; var featuredIds = ['footerB', 'headerA', 'landingA']; var result = filterBy(features, featuredIds); console.log(result); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.6/lodash.min.js"></script> 

Or if you can use ES6, a combination of Array.prototype.filter() and Set : 或者,如果可以使用ES6,则可以使用Array.prototype.filter()Set

 const filterBy = (arr, filters) => { const filtersSet = new Set(filters); return arr.filter((item) => filtersSet.has(item.uId)); }; const featuredIds = ['footerB', 'headerA', 'landingA']; const features = [{ "title": "first footer", "section": "structure", "categoryId": "footer", "uId": "footerA" }, { "title": "second footer", "section": "structure", "categoryId": "footer", "uId": "footerB" }, { "title": "first header", "section": "structure", "categoryId": "header", "uId": "headerA" }, { "title": "first header", "section": "structure", "categoryId": "header", "uId": "headerB" }, { "title": "first landing", "section": "structure", "categoryId": "landing", "uId": "landingA" }, { "title": "second landing", "section": "structure", "categoryId": "landing", "uId": "landingB" }, { "title": "third landing", "section": "structure", "categoryId": "landing", "uId": "landingC" }, { "title": "first nav", "section": "structure", "categoryId": "navigation", "uId": "navA" }, { "title": "first footer", "section": "components", "categoryId": "blog", "uId": "blogA" }, { "title": "second footer", "section": "components", "categoryId": "blog", "uId": "blogB" }, { "title": "first header", "section": "components", "categoryId": "contact_button", "uId": "contact_buttonA" }, { "title": "first landing", "section": "components", "categoryId": "content_bloc", "uId": "content_blocA" }, { "title": "second landing", "section": "components", "categoryId": "content_bloc", "uId": "content_blocB" }, { "title": "third landing", "section": "components", "categoryId": "content_bloc", "uId": "content_blocC" }, { "title": "first nav", "section": "components", "categoryId": "cover", "uId": "coverA" }]; const result = filterBy(features, featuredIds); console.log(result); 

Another lodash option is _.intersectionWith() : 另一个lodash选项是_.intersectionWith()

 function filterBy(arr, filters) { return _.intersectionWith(arr, filters, function(value, filter) { return value.uId === filter; }); } var features = [{ "title": "first footer", "section": "structure", "categoryId": "footer", "uId": "footerA" }, { "title": "second footer", "section": "structure", "categoryId": "footer", "uId": "footerB" }, { "title": "first header", "section": "structure", "categoryId": "header", "uId": "headerA" }, { "title": "first header", "section": "structure", "categoryId": "header", "uId": "headerB" }, { "title": "first landing", "section": "structure", "categoryId": "landing", "uId": "landingA" }, { "title": "second landing", "section": "structure", "categoryId": "landing", "uId": "landingB" }, { "title": "third landing", "section": "structure", "categoryId": "landing", "uId": "landingC" }, { "title": "first nav", "section": "structure", "categoryId": "navigation", "uId": "navA" }, { "title": "first footer", "section": "components", "categoryId": "blog", "uId": "blogA" }, { "title": "second footer", "section": "components", "categoryId": "blog", "uId": "blogB" }, { "title": "first header", "section": "components", "categoryId": "contact_button", "uId": "contact_buttonA" }, { "title": "first landing", "section": "components", "categoryId": "content_bloc", "uId": "content_blocA" }, { "title": "second landing", "section": "components", "categoryId": "content_bloc", "uId": "content_blocB" }, { "title": "third landing", "section": "components", "categoryId": "content_bloc", "uId": "content_blocC" }, { "title": "first nav", "section": "components", "categoryId": "cover", "uId": "coverA" }]; var featuredIds = ['footerB', 'headerA', 'landingA']; var result = filterBy(features, featuredIds); console.log(result); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.6/lodash.min.js"></script> 

You can do this with filter() and includes() in plain js. 您可以使用普通js中的filter()includes()来做到这一点。

 var data = [{"title":"first footer","section":"structure","categoryId":"footer","uId":"footerA"},{"title":"second footer","section":"structure","categoryId":"footer","uId":"footerB"},{"title":"first header","section":"structure","categoryId":"header","uId":"headerA"},{"title":"first header","section":"structure","categoryId":"header","uId":"headerB"},{"title":"first landing","section":"structure","categoryId":"landing","uId":"landingA"},{"title":"second landing","section":"structure","categoryId":"landing","uId":"landingB"},{"title":"third landing","section":"structure","categoryId":"landing","uId":"landingC"},{"title":"first nav","section":"structure","categoryId":"navigation","uId":"navA"},{"title":"first footer","section":"components","categoryId":"blog","uId":"blogA"},{"title":"second footer","section":"components","categoryId":"blog","uId":"blogB"},{"title":"first header","section":"components","categoryId":"contact_button","uId":"contact_buttonA"},{"title":"first landing","section":"components","categoryId":"content_bloc","uId":"content_blocA"},{"title":"second landing","section":"components","categoryId":"content_bloc","uId":"content_blocB"},{"title":"third landing","section":"components","categoryId":"content_bloc","uId":"content_blocC"},{"title":"first nav","section":"components","categoryId":"cover","uId":"coverA"}]; var featuredIds = ['footerB', 'headerA', 'landingA']; var result = data.filter(function(e) { return featuredIds.includes(e.uId); }) console.log(result) 

暂无
暂无

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

相关问题 如何遍历对象数组并使用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 如何检查数组的至少一项是否包含在 object 的数组中并遍历所有对象(JS) - How to check if at least one item of an array is included in an array of an object and loop through all objects (JS) 如何基于其他数组遍历数组中的每个值并使用 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 add new key value pair when loop through array of objects in lodash 如何使用lodash在数组中查找具有值的对象 - How to find object with value in array using lodash LoDash从子数组中找到其属性与对象数组中的值匹配的对象 - LoDash find the object whose property matches the value from array of objects with children array 如何将数组中的对象值复制到 javascript/lodash 中的另一个空数组? - How to copy object value in array to another empty array in javascript/lodash? 检查lodash中的对象数组的属性值 - Check array of objects in lodash for property value 如何检查数组对象的属性是否与另一个 object 数组中的值之一匹配 - How to check if property of an objects of array matches with one of the values in another array of object lodash:通过另一个JSON对象过滤对象数组 - lodash:Filtering Array of objects by another JSON Object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM