首页 > 解决方案 > 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).

标签: javascriptarraysobject

解决方案


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);


推荐阅读