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

232
Vistas
how to get the value by the help of includes/index of

how to get the value in any way if key does match with "Item"

   const data = [{
            "Item-55566": "phone",        
        },
        {
            "Items-44555": "Case",       
        }
    ];
    
    /* How to get value if index found by Item */
    
    for(let i = 0; i<data.length; i++) {
        console.log(data[i].includes("Item"));
        //Expecting phone and case
    }
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

for-in allows you to loop through the keys in an object. Not to be confused with for-of, which loop through elements in an array.

const data = [{
          "Item-55566": "phone",        
      },
      {
          "Items-44555": "Case",       
      }
 ];
 
for(let datum of data)
{
  for(let key in datum)
  {
    if(key.includes("Item"))
    {
      console.log(datum[key]);
    }
  }
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

If you want to keep your loop (good that it's only one loop compared to other answers) you can do it with Object.keys and values:

   const data = [{
            "Item-55566": "phone",        
        },
        {
            "Items-44555": "Case",       
        }
    ];
    
    /* How to get value if index found by Item */
    
    for(let i = 0; i<data.length; i++) {
        if(Object.keys(data[i])[0].includes('Item')){
            console.log(Object.values(data[i])[0]);
        }
    }

about 4 years ago · Juan Pablo Isaza Denunciar

0

In the simple way just change data[i].includes("Item") to data[i].keys().includes("Item").

BUT! Could we have some alternative data set here? For example:

const data = [{
        "Item-55566": "phone",
        "SomeKey: "Some Value",
        123123: "Numeric key with value"
    },
    {
        "Items-44555": "Case",
        "Another-key": "Another value"
    }
];

In this case you need to put some changes in your code to find correct keys & values:

for(let i = 0; i<data.length; i++) {
    data[i].keys().forEach(v=>{ 
      String(v).includes("Item") && console.log("Got index: ${i}, key: ${v}, value: ${data[i][v]}") 
    })

}
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