javascript - How to find a value in a nested array and merge it with a value from a higher array level?
问题描述
I have been trying for some time to find the currently active filter OptionCode
and merge it together with the FilterCode
into a new object. The OptionCode
is deeper nested in the Array than the FilterCode
.
filters = [
{
FilterCode: "TourPrice", // Take that filter code
FilterName: "Tour Price",
Options: [
{ Name: "Free", OptionCode: "Free", active: false, blocked: false },
{ Name: "Paid", OptionCode: "Paid", active: false, blocked: false },
{
Name: "Free and Paid",
OptionCode: "FreeAndPaid", // take that Option Code
active: true, //<- active
blocked: false,
},
],
},
{
FilterCode: "SortedBy", // Take that filter code
FilterName: "Sorted By",
Options: [
{
Name: "Most Relevant",
OptionCode: "MostRelevant",// Take that OptionCode Here
active: true, // <- active
blocked: false,
},
{
Name: "Latest Tour",
OptionCode: "LatestTour",
active: false,
blocked: false,
},
{
Name: "Oldest Tour",
OptionCode: "OldestTour",
active: false,
blocked: false,
},
{
Name: "Lowest Price",
OptionCode: "LowestPrice",
active: false,
blocked: false,
},
],
},
],
The result should look like that const activeFilters = {TourPrice: "FreeAndPaid", SortedBy: "MostRelevant" }
. The first value is the FilterCode
the second the currently active OptionCode
. Only one option is active at a time per filter (i.e. never zero or never more than one options can be active).
解决方案
You can use .map()
on your filter
array to create an array of [key, value]
pairs. The key here is the FilterCode
from each object, and the value
here is the Name property from the object obtained by calling .find()
on your Options array, and finding the object with an active
property set to true
. You can then wrap this array of key-value pairs into a call to Object.fromEntries()
to build your object:
const filters = [{ FilterCode: "TourPrice", FilterName: "Tour Price", Options: [{ Name: "Free", OptionCode: "Free", active: false, blocked: false }, { Name: "Paid", OptionCode: "Paid", active: false, blocked: false }, { Name: "Free and Paid", OptionCode: "FreeAndPaid", active: true, blocked: false, }, ], }, { FilterCode: "SortedBy", FilterName: "Sorted By", Options: [{ Name: "Most Relevant", OptionCode: "MostRelevant", active: true, blocked: false, }, { Name: "Latest Tour", OptionCode: "LatestTour", active: false, blocked: false, }, { Name: "Oldest Tour", OptionCode: "OldestTour", active: false, blocked: false, }, { Name: "Lowest Price", OptionCode: "LowestPrice", active: false, blocked: false, }, ], }, ];
const res = Object.fromEntries(filters.map(({FilterCode, Options}) => [
FilterCode, Options.find(obj => obj.active).Name
]));
console.log(res);
推荐阅读
- javascript - Vue.js 如何使用选项卡加载不同的组件
- vba - MS Access 宏将文件作为 .xlsx 导出到特定文件路径,并将当前日期 (YYMMDD) 添加到文件名末尾
- c# - 管理个人资料页面:尝试激活时无法解析服务类型
- python - Python Django:您正在使用 staticfiles 应用程序而没有将 STATIC_ROOT 设置设置为文件系统路径
- javascript - javascript UTF-16 或 UTF-8 中的字符串编码
- amazon-web-services - 如何基于 kafka 队列自动扩展 AWS EKS 实例
- angular - 在 Angular 8 中的兄弟组件之间传递数据的最佳方式是什么?
- php - 如何在 Laravel 7 的 RegisterController 中将注册表单中的角色分配给新用户?
- asp.net-mvc - Kendo UI 下拉菜单显示为文本框
- javascript - 如何在本地存储 (JS) HTML 按钮并在页面加载时检索按钮