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

162
Vistas
How to looping trough an array of object when array name is not consistent?

Consider below example:

const obj ={
price:[{multiple items},{multiple items}],
name:"",
id:"",
}

i want to apply foreach on price property but the issue is the property name is changing wrt cases like saleprice , taxprice etc.

if i am using obj.keys() method it doesn't work because it returns key as a string.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can try this:

const obj = {
  price: [10, 20, 30],
  name: "",
  id: "",
};

const pricePropertyName = Object.keys(obj).find((prop) => prop.includes('price'));
if (pricePropertyName) {
  obj[pricePropertyName].forEach(el => {
    console.log(el);
  });
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

const key = Object.keys(obj).find((key) => key.indexOf('price') > -1)
obj[key].forEach(price => console.log(price))
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use "Object.values()" or "Object.fromEntries()" to get the values or both keys & values.

const obj ={
price:[{multiple items},{multiple items}],
name:"",
id:"",
}

const priceArray = Object.values(obj).find( value => Array.isArray(value))

FYI

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values

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