2

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]

1 Answer 1

1

You need first check for the entire catalogCategories not only the first element product.catalogCategories[0] by using a map and use new Map to keep track of not duplicate categories.

const list = [
    {
        "title": "First",
        "catalogCategories": [ "sport", "economy" ]
    },
    {
        "title": "Second",
        "catalogCategories": [ "politics", "tourism" ]
    },
    {
        "title": "Third",
        "catalogCategories": [ "sport", "universe" ]
    },
    {
        "title": "Fourth",
        "catalogCategories": [ "economy", "politics" ]
    }
]
let categoryDataMap = new Map();
for (let product of list) {
if (product.catalogCategories.length > 0) {
      product.catalogCategories.map(catalog=>{
        if(!categoryDataMap.has(catalog)){
            categoryDataMap.set(catalog,{"name": "catalogCategories", "label": catalog, "selected": false, "value": catalog})
        }
        })
    }
}
const categoryData = Array.from(categoryDataMap.values())
console.log(categoryData)

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

2 Comments

I understand and it is right but it is not following my code, and it gives error in my project, can i have the answer following my logic? For example, I can not change categoryData = [ ] to categoryDataMap = New Map(). Thanks
Yes of course you can just assign values of categoryDataMap to categoryData.

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.