JSON into Map & Array
TypeScript Model
- JSON
- Result 1
- Result 2
const animalFilters = {
appliedSearchFilters: [
{
filterKey: "animalType",
filterType: "CHAIN",
appliedOptions: [
{
optionName: "Dog",
optionId: "DOG",
optionValue: "dog",
optionGroupName: null,
count: 500,
children: null,
properties: null
},
{
optionName: "Cat",
optionId: "CAT",
optionValue: "cat",
optionGroupName: null,
count: 300,
children: null,
properties: null
},
{
optionName: "Horse",
optionId: "HORSE",
optionValue: "Horse",
optionGroupName: null,
count: 150,
children: null,
properties: null
}
]
},
{
filterKey: "animalBreed",
filterType: "CHAIN",
appliedOptions: [
{
optionName: "Labrador",
optionId: null,
optionValue: "labrador",
optionGroupName: null,
count: 150,
children: null,
properties: null
},
{
optionName: "Persian",
optionId: null,
optionValue: "persian",
optionGroupName: null,
count: 100,
children: null,
properties: null
}
]
},
{
filterKey: "animalStatus",
filterType: "BOOLEAN",
appliedOptions: [
{
optionId: "AVAILABLE",
optionName: "Available",
optionValue: "available"
},
{
optionId: "ADOPTED",
optionName: "Adopted",
optionValue: "adopted"
}
]
}
]
};
// Output: [string, string[]][];
[
["animalType", ["Dog", "Cat", "Horse"]],
["animalBreed", ["Labrador", "Persian"]]
]
// Output of resultArray2 - [string, string][];
[
["animalType", "Animal Type: Dog; Cat; Horse"],
["animalBreed", "Animal Breed: Labrador; Persian"],
["animalStatus", "Animal Status: Available | Adopted"]
]
Map<string, string[]>
To convert the given object into the desired format using Map<string, string[]> in TypeScript and
skip the filters with filterType as BOOLEAN,
you can follow these steps:
- Create a new
Map<string, string[]>. - Iterate over the
appliedSearchFiltersarray. - For each filter, check if the
filterTypeis notBOOLEAN. - For each filter, extract the
filterKeyand theoptionNamevalues. - Add these values to the map.
const resultMap1 = new Map<string, string[]>();
animalFilters.appliedSearchFilters.forEach(filter => {
if (filter.filterType !== 'BOOLEAN') {
const key = filter.filterKey;
const values = filter.appliedOptions.map(option => option.optionName);
resultMap1.set(key, values);
}
});
// Convert the Map to the desired array format i.e) [string, string[]][];
const resultArray1 = Array.from(resultMap1.entries());
console.log(resultMap1);
// Output of resultMap1 - Map<string, string[]>;
Map (2) {
"animalType" => ["Dog", "Cat", "Horse"],
"animalBreed" => ["Labrador", "Persian"]
}
console.log(resultArray1);
// Output of resultArray1 - [string, string[]][];
[
["animalType", ["Dog", "Cat", "Horse"]],
["animalBreed", ["Labrador", "Persian"]]
]
Map<string, string>
To convert the given object into the desired format using Map<string, string> in TypeScript,
you can follow these steps:
- Create a new
Map<string, string>. - Iterate over the
appliedSearchFiltersarray. - Extract the
filterKeyand theoptionNamevalues. - Format the values as required and add them to the map.
const resultMap2 = new Map<string, string>();
animalFilters.appliedSearchFilters.forEach(filter => {
const key = filter.filterKey;
const values = filter.appliedOptions.map(option => option.optionName).join('; ');
let formattedValue: string;
switch (key) {
case 'animalType':
formattedValue = `Animal Type: ${values}`;
break;
case 'animalBreed':
formattedValue = `Animal Breed: ${values}`;
break;
case 'animalStatus':
formattedValue = `Animal Status: ${values.replace('; ', ' | ')}`;
break;
default:
formattedValue = values;
}
resultMap2.set(key, formattedValue);
});
// Convert the Map to the desired array format i.e) [string, string][];
const resultArray2 = Array.from(resultMap2.entries());
console.log(resultArray2);
console.log(resultMap2);
// Output of resultMap2 - Map<string, string>;
Map (3) {
"animalType" => "Animal Type: Dog; Cat; Horse",
"animalBreed" => "Animal Breed: Labrador; Persian",
"animalStatus" => "Animal Status: Available | Adopted"
}
console.log(resultArray2);
// Output of resultArray2 - [string, string][];
[
["animalType", "Animal Type: Dog; Cat; Horse"],
["animalBreed", "Animal Breed: Labrador; Persian"],
["animalStatus", "Animal Status: Available | Adopted"]
]
Array of Arrays into Array of Objects
// Filter and map the array to include only the desired keys
const convertedArray = inputArray
.filter(([key]) => key !== "fruitStatus" && key !== "fruitIsSeasonal")
.map(([key, value]) => ({ key, value }));
// input
const inputArray = [
["fruitName", "Name: Apple"],
["fruitColor", "Color: Red"],
["fruitStatus", "Status: Fresh"],
["fruitIsSeasonal", "IsSeasonal: true"]
];
// Output
[
{ key: "fruitName", value: "Name: Apple" },
{ key: "fruitColor", value: "Color: Red" }
]
// input
const inputArray = [
["fruitName", ["Apple"]],
["fruitColor", ["Red"]],
["fruitStatus", ["Fresh"]],
["fruitIsSeasonal", ["true"]]
];
// Output
[
{ key: "fruitName", value: ["Apple"] },
{ key: "fruitColor", value: ["Red"] }
]