0

I am working on nested array and try to find a value inside the nested array based on another value. I want to find the OptionCode of the currently active filters.

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, //Find OptionCode Here
            blocked: false,
          },
        ],
      },
      {
        FilterCode: "SortedBy",
        FilterName: "Sorted By",
        Options: [
          {
            Name: "Most Relevant",
            OptionCode: "MostRelevant",
            active: true, // Find OptionCode Here
            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,
          },
        ],
      },
    ],

For the above array the expected outcome should be somthing like ["FreeAndPaid","MostRelevant"]

4 Answers 4

2

If every filter has an active option, you can use

console.log(filters.map(filter => filter.Options.find(option => option.active).OptionCode))

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, //Find OptionCode Here
            blocked: false,
          },
        ],
      },
      {
        FilterCode: "SortedBy",
        FilterName: "Sorted By",
        Options: [
          {
            Name: "Most Relevant",
            OptionCode: "MostRelevant",
            active: true, // Find OptionCode Here
            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,
          },
        ],
      },
    ]
console.log(filters.map(filter => filter.Options.find(option => option.active).OptionCode))

Sign up to request clarification or add additional context in comments.

Comments

1

You can use high order function: Reduce, Map and Filter for a clean one-line result:

let 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, //Find OptionCode Here
            blocked: false,
          },
        ],
      },
      {
        FilterCode: "SortedBy",
        FilterName: "Sorted By",
        Options: [
          {
            Name: "Most Relevant",
            OptionCode: "MostRelevant",
            active: true, // Find OptionCode Here
            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,
          },
        ],
      },
    ];
    
    
 let output = filters.reduce((out, f) => out.concat(f.Options.filter(o => o.active).map(o => o.OptionCode)), []);
 
 console.log(output)

Comments

0

1:- Get all options first 2:- Then you can filter options like this

const allOptions = filters.map(filter => (...filter.options) )

const requiredOptionCodes = options.filter(option => option.active === true )

Comments

0

To build on Renu Yadav's answer: If you want to return OptionCodes only, add a .map() to get OptionCode from all the options:

const requiredOptionCodes = options.filter(option => option.active === true ).map((option) => {return option['OptionCode']})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.