Javascript 如何循环遍历对象数组,并使用lodash检查该对象中的值是否与另一个数组中的项匹配
我有一系列的项目,这些项目是如下所示的特色组件:Javascript 如何循环遍历对象数组,并使用lodash检查该对象中的值是否与另一个数组中的项匹配,javascript,arrays,lodash,Javascript,Arrays,Lodash,我有一系列的项目,这些项目是如下所示的特色组件: var featuredIds = ['footerB', 'headerA', 'landingA']; [{ "title": "first footer", "section": "structure", "categoryId": "footer", "uId": "footerA" },{ "title": "second footer", "section": "structure
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"
}]
[{
"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"
}]
我还有一个对象数组,看起来像这样:
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"
}]
[{
"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"
}]
我想创建一个新的数组,该数组只保存与我在featureid
数组中提供的featureid匹配的组件,该数组如下所示:
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"
}]
[{
"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"
}]
我已经考虑过使用0.some、0.find和其他一些工具,但没有得到我想要的结果。我已经用双for循环写了这篇文章,这就是为什么我想让lodash去掉它/学习一些新的东西。你可以用纯js中的
filter()
和includes()
来实现这一点
var data=[{“title”:“first footer”,“section”:“structure”,“categoryId”:“footer”,“uId”:“footerA”},{“title”:“second footer”,“section”:“structure”,“categoryId”:“header”,“uId”:“header”,“section”:“structure”,“categoryId”:“header”,“uId”:“header”,“section”:“structure”,“categoryId”:“header”,“uId”:“header”,“uId”:“机头B”},{“标题”:“第一次着陆”、“部分”:“结构”、“类别ID”:“着陆”、“uId”:“着陆A”},{“标题”:“第二次着陆”、“部分”:“结构”、“类别ID”:“着陆”、“uId”:“着陆B”},{“标题”:“第三次着陆”、“部分”:“结构”、“类别ID”:“着陆”、“uId”:“着陆C”},{“标题”:“第一次导航”、“部分”:“结构”、“类别ID”:“导航”、“uId”:“导航”“:”navA“},{”标题“:”第一页脚“,”章节“:”组件“,”分类“:”博客“,”uId“:”博客“,”第二页脚“,”章节“,”组件“,”分类“:”博客“,”uId“,”第一页眉“,”章节“:”组件“,”分类“:”联系人按钮“,”uId“:”联系人按钮“},{”标题“:”第一次登录“,”章节“:”组件“,”分类“:”“内容组”、“uId”:“内容组”、“标题”:“第二次登陆”、“章节”:“组件”、“类别ID”:“内容组”、“uId”:“内容组”、“uId”:“第三次登陆”、“章节”:“组件”、“类别ID”:“内容组”、“uId”:“内容组”、“uId”:“内容组”、“标题”:“第一次登陆”、“章节”:“组件”、“类别ID”:“封面”、“uId”:“封面”);
var featuredIds=['footerB'、'headerA'、'landingA'];
var结果=data.filter(函数(e){
返回特性包括(如uId);
})
log(result)
您可以使用普通js中的filter()
和includes()
来实现这一点
var data=[{“title”:“first footer”,“section”:“structure”,“categoryId”:“footer”,“uId”:“footerA”},{“title”:“second footer”,“section”:“structure”,“categoryId”:“header”,“uId”:“headerA”},{“title”:“first header”,“section”:“structure”,“categoryId”:“header”,“uId”:“header”,“section”:“structure”,“categoryId”:“header”,“uId”:”机头B},{“标题”:“第一次着陆”、“部分”:“结构”、“分类ID”:“着陆”、“uId”:“着陆A”},{“标题”:“第二次着陆”、“部分”:“结构”、“分类ID”:“着陆”、“uId”:“着陆B”},{“标题”:“第三次着陆”、“部分”:“结构”、“分类ID”:“着陆”、“uId”:“着陆C”},{“标题”:“第一次导航”、“部分”:“结构”、“分类ID”:“导航”、“uId”:“着陆C”}“:”navA“},{”标题“:”第一页脚“,”章节“:”组件“,”分类“:”博客“,”uId“:”博客“,”第二页脚“,”章节“,”组件“,”分类“:”博客“,”uId“,”第一页眉“,”章节“:”组件“,”分类“:”联系人按钮“,”uId“:”联系人按钮“},{”标题“:”第一次登录“,”章节“:”组件“,”分类“:”“内容组”、“uId”:“内容组”、“标题”:“第二次登陆”、“章节”:“组件”、“类别ID”:“内容组”、“uId”:“内容组”、“uId”:“第三次登陆”、“章节”:“组件”、“类别ID”:“内容组”、“uId”:“内容组”、“uId”:“内容组”、“标题”:“第一次登陆”、“章节”:“组件”、“类别ID”:“封面”、“uId”:“封面”);
var featuredIds=['footerB'、'headerA'、'landingA'];
var结果=data.filter(函数(e){
返回特性包括(如uId);
})
console.log(result)
您可以将lodash的链与.keyBy()
和.at()一起使用:
功能过滤器by(arr,过滤器){
返回(功能)
.keyBy('uId')
.at(过滤器)
.value();
}
变量特征=[{
“标题”:“第一个页脚”,
“部分”:“结构”,
“类别ID”:“页脚”,
“uId”:“footerA”
}, {
“标题”:“第二个页脚”,
“部分”:“结构”,
“类别ID”:“页脚”,
“uId”:“页脚B”
}, {
“标题”:“第一个标题”,
“部分”:“结构”,
“类别ID”:“标题”,
“uId”:“headerA”
}, {
“标题”:“第一个标题”,
“部分”:“结构”,
“类别ID”:“标题”,
“uId”:“headerB”
}, {
“标题”:“首次着陆”,
“部分”:“结构”,
“类别ID”:“着陆”,
“uId”:“兰廷加”
}, {
“标题”:“第二次着陆”,
“部分”:“结构”,
“类别ID”:“着陆”,
“uId”:“landingB”
}, {
“头衔”:“第三次登陆”,
“部分”:“结构”,
“类别ID”:“着陆”,
“uId”:“landingC”
}, {
“标题”:“第一导航”,
“部分”:“结构”,
“类别ID”:“导航”,
“uId”:“navA”
}, {
“标题”:“第一个页脚”,
“部分”:“组件”,
“分类”:“博客”,
“uId”:“blogA”
}, {
“标题”:“第二个页脚”,
“部分”:“组件”,
“分类”:“博客”,
“uId”:“blogB”
}, {
“标题”:“第一个标题”,
“部分”:“组件”,
“类别ID”:“联系按钮”,
“uId”:“联系按钮”
}, {
“标题”:“首次着陆”,
“部分”:“组件”,
“类别ID”:“内容集团”,
“uId”:“content\u blocA”
}, {
“标题”:“第二次着陆”,
“部分”:“组件”,
“类别ID”:“内容集团”,
“uId”:“content\u blocB”
}, {
“头衔”:“第三次登陆”,
“部分”:“组件”,
“类别ID”:“内容集团”,
“uId”:“content\u blocC”
}, {
“标题”:“第一导航”,
“部分”:“组件”,
“分类”:“封面”,
“uId”:“coverA”
}];
var featuredIds=['footerB'、'headerA'、'landingA'];
var结果=过滤比(特征、特征);
console.log(结果);
您可以将lodash的链与.keyBy()
和.at()
一起使用:
功能过滤器by(arr,过滤器){
返回(fe)