javascript - 比较两个数组并过滤它们 javascript
问题描述
基本上我有两个数组,我用它来配置按钮。
第一个数组,它定义了应按顺序显示的按钮数量。
buttonGroups: [ 0, 2 ]
另一个关于实际按钮的对象数组。
buttons = [
{
buttonLabel: 'label1',
cond1: true,
cond2: false
},
{
buttonLabel: 'label2',
cond1: true,
cond2: false
},
{
buttonLabel: 'label3',
cond1: false,
cond2: true
}
];
是buttonGroups
配置数组。如果它只有[0, 1]
那么前两个按钮将存在。如果buttonGroups
只有我们应该在数组中[0, 3]
存在第一个和第三个按钮。buttons
这是我尝试过的
buttonGroups.map((payload1, index1) => {
buttons .map((payload2, index2) => {
if(index1 === index2){
//Display Here only the matched index from ButtonGroups
console.log(payload2)
}
})
})
这是第一个索引按钮数组。如何获得匹配的数组按钮?
解决方案
给你一个解决方案
var buttonGroups = [ 0, 2 ];
var buttons = [
{
buttonLabel: 'label1',
cond1: true,
cond2: false
},
{
buttonLabel: 'label2',
cond1: true,
cond2: false
},
{
buttonLabel: 'label3',
cond1: false,
cond2: true
}
];
var filteredButtons = buttonGroups.map(item => {
return buttons[item];
});
console.log(filteredButtons);
filteredButtons
将返回您可以呈现的过滤按钮。
推荐阅读
- android - 如何在布局预览中将格式化文本显示为工具:文本?
- dart - 如何在 vscode 中使用 code-runner 运行 dart 代码
- python-3.x - Tkinter 窗口在一次运行后自动关闭
- python-3.x - 如何提取其母亲包含特定文本的元素?
- reactjs - router5 以编程方式导航浏览器历史记录
- javascript - 使用 Javascript 避免在 firebase 中重复
- javascript - cloudinary.v2.api.delete_folder 的替代方案是什么?由于不推荐使用此方法,我现在如何删除空文件夹?
- r - 用 n 个副本替换 data.frame 中的每个观察值
- akka - 了解在上游失败时如何处理 akka 引用
- python - 对 opencv 直播流应用多重效果