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

标签 javascript arrays lodash

我有一系列具有如下特色组件的项目:

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

我还有一个看起来像这样的对象数组:

[{
    "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"
  }]

我想创建一个新数组,它只保存与我在 featureIds 数组中提供的 featureId 相匹配的组件,如下所示:

[{
    "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"
  }]

我已经考虑过使用 _.some、_.find 和其他一些方法,但无法得到我正在寻找的结果。我已经使用双 for 循环编写了此内容,这就是为什么我希望我们 lodash 删除它/学习新的东西。

最佳答案

您可以将 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>

或者,如果您可以使用 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);

另一个 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>

关于javascript - 如何使用 lodash 循环遍历对象数组并检查该对象中的值是否与另一个数组中的项目匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40506469/

相关文章:

javascript - 以编程方式触发 Kendo Chart.seriesClick 事件

arrays - 多维数组中的多重赋值

python - 一次删除numpy数组中某个值的元素

javascript - 在 react 中生成一个唯一的 id

javascript - 在对象数组javascript中查找并替换值

javascript - 使用范围数据进行 Angular ng-pattern 正则表达式验证

javascript - Activity 按钮

javascript - 在页面加载或刷新时加载随机js文件

c - 通过 ref 从结构体中的数组传回值

javascript - Lodash 按值对深层对象进行排序和排序,而不丢失 key