javascript - 如何在vue中通过另一个数组过滤数组
问题描述
我有两个对象数组,其中一个称为子模块,其中有一个子数组,我需要通过访问的对象数组过滤这些子数组
new Vue({
data: {
submodules: [
{
type: "something1",
children: [
{
name: "new_sth1",
value: "new_sth1"
},
{
name: "new_sth2",
value: "new_sth2"
}
]
},
{
type: "something2",
children: [
{
name: "new_sth3",
value: "new_sth3"
},
]
},
{
type: "something3",
children: [
{
name: "new_sth4",
value: "new_sth4"
},
{
name: "new_sth5",
value: "new_sth5"
},
{
name: "new_sth6",
value: "new_sth6"
},
]
},
],
accessed: [
{value: 'new_sth1'},
{value: 'new_sth2'},
{value: 'new_sth3'},
{value: 'new_sth4'},
]
}
})
<div class="row">
<div class="col-6 mt-3" v-for="item in submodules">
<div class="card" style="min-height: 260px">
<div class="card-header" style="background: #757575;color: white">
{{item.type}}
</div>
<div class="card-body">
<ul class="nav nav-pills nav-stacked mb-0 pb-0">
<li style="width: 100%;cursor: pointer" class="navbar my-0 py-0" v-for="child in item.children">
<a style="color: black" :href="'/'+child.value">
<span class="text-right navbar-text">{{child.name}}</span>
</a>
</li>
</ul>
</div>
</div>
</div>
</div>
我需要通过访问的数组值过滤子模块数组。我尝试了很多方法,但我无法解决问题。如果有人有任何想法,请与我分享。
解决方案
您可以使用computed
属性来根据 过滤submodules
数组accessed
。
codeandbox:代码没有样式,因为我没有安装引导程序。
computed: {
getFilteredResult() {
return this.submodules.reduce((acc, curr) => {
const children = curr.children.filter(({ value }) => {
return this.accessed.find((x) => x.value === value);
});
acc.push({ ...curr, children });
return acc;
}, []);
},
},
推荐阅读
- mongodb - 如何在给定 MongoDB 中的元素位置的情况下提取数组的切片?
- python - 如何从头开始训练 ssd-mobilenet
- string - grepping 文本字符串不返回结果
- swift - 每日本地通知工作了一天,然后停止了 [Swift]
- c++ - C++ 控制台仅在请求时显示输入
- c++ - C++ 错误中的 switch case 和 enum - 表达式必须具有常量值
- python - 需要登录时,自定义登录后 Django 不会重定向到“下一步”
- java - Intellij Idea 依赖类已导入但无法实例化
- ios - 使用 Swift 将日期转换为设备时区
- google-bigquery - BigQuery - 通过 CLI 计划的查询更新删除