Home > Net >  Javascript check and push dynamically elements in a new array
Javascript check and push dynamically elements in a new array

Time:10-25

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]

CodePudding user response:

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)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related