Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

92
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda