I have a list of elements like the following data
[
{
"title": "First",
"catalogCategories": [ "sport", "economy" ]
},
{
"title": "Second",
"catalogCategories": [ "politics", "tourism" ]
},
{
"title": "Third",
"catalogCategories": [ "sport", "universe" ]
},
{
"title": "Fourth",
"catalogCategories": [ "economy", "politics" ]
}
]
I am checking for each element of the list if the catalogCategories array exists and if it does i push in a catalogData array the category of any element only once, because it happens that the category for any element can be the same, i am filtering in the catalogData array all the categories of the elements in catalogCategories
This is the following code
let categoryData = [];
let isCategory = false
for (let product of list) {
if (product.catalogCategories[0].length > 0) {
isCategory = true
let isFound = false
for (let i = 0; i < categoryData.length; i++) {
if (categoryData[i].value === product.catalogCategories[0] ||
categoryData[i].value === product.catalogCategories[1] ) {
isFound = true;
}
}
if (!isFound) {
categoryData.push({"name": "catalogCategories", "label": product.catalogCategories[0], "selected": false, "value": product.catalogCategories[0]})
}
}
My problem is here
if (!isFound) {
categoryData.push({"name": "catalogCategories", "label": product.catalogCategories[0], "selected": false, "value": product.catalogCategories[0]})
}
As you can see i always only push the first element of the catalogCategories after all these checks, how can i write down that dynamically i can push the first and / or the second ?
"value": product.catalogCategories[0]
"label": product.catalogCategories[0]