Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

90
Views
Javascript check and push dynamically elements in a new array

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]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!