I need to search for a value within a nested array of objects and update it. Specifically, in the given array, I want to locate the OptionCode
based on a String and mark the corresponding option as blocked
. The search strings are stored in an array.
Initial Array:
filters: [
{
FilterCode: "TourPrice",
FilterName: "Tour Price",
FilterIcon: show_Dollar,
Options: [
{
Name: "Free",
OptionCode: "Free",
active: false,
blocked: false,
forbiddenFilterOptionCode: ["LowestPrice"],
},
{
Name: "Paid",
OptionCode: "Paid",
active: false,
blocked: false,
forbiddenFilterOptionCode: [],
},
{
Name: "Free and Paid",
OptionCode: "FreeAndPaid",
active: true,
blocked: false,
forbiddenFilterOptionCode: [],
},
],
},
{
FilterCode: "SortedBy",
FilterName: "Sorted By",
FilterIcon: show_Sort,
Options: [
{
Name: "Most Relevant",
OptionCode: "MostRelevant",
active: true,
blocked: false,
forbiddenFilterOptionCode: [],
},
{
Name: "Latest Tour",
OptionCode: "LatestTour",
active: false,
blocked: false,
forbiddenFilterOptionCode: [],
},
{
Name: "Oldest Tour",
OptionCode: "OldestTour",
active: false,
blocked: false,
forbiddenFilterOptionCode: [],
},
{
Name: "Lowest Price",
OptionCode: "LowestPrice",
active: false,
blocked: false,
forbiddenFilterOptionCode: ["Free", "FreeAndPaid'],
},
],
},
],
For the input search
["Free", "FreeAndPaid"]
, the resulting array should be as follows.
Intended Result Array
filters: [
{
FilterCode: "TourPrice",
FilterName: "Tour Price",
FilterIcon: show_Dollar,
Options: [
{
Name: "Free",
OptionCode: "Free", // Find OptionCode
active: false,
blocked: true, // set blocked to true
forbiddenFilterOptionCode: ["LowestPrice"],
},
{
Name: "Paid",
OptionCode: "Paid",
active: false,
blocked: false,
forbiddenFilterOptionCode: [],
},
{
Name: "Free and Paid",
OptionCode: "FreeAndPaid", // Find OptionCode
active: true,
blocked: true, // set blocked to true
forbiddenFilterOptionCode: [],
},
],
},
{
FilterCode: "SortedBy",
FilterName: "Sorted By",
FilterIcon: show_Sort,
Options: [
{
Name: "Most Relevant",
OptionCode: "MostRelevant",
active: true,
blocked: false,
forbiddenFilterOptionCode: [],
},
{
Name: "Latest Tour",
OptionCode: "LatestTour",
active: false,
blocked: false,
forbiddenFilterOptionCode: [],
},
{
Name: "Oldest Tour",
OptionCode: "OldestTour",
active: false,
blocked: false,
forbiddenFilterOptionCode: [],
},
{
Name: "Lowest Price",
OptionCode: "LowestPrice",
active: false,
blocked: false,
forbiddenFilterOptionCode: ["Free", "FreeAndPaid"],
},
],
},
],