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

148
Vistas
How array will get assign to new key with in the same key with some more object?

I am trying to add existing key value to the new key wrt to the same key, I might not be able to explain it clearly with my words, but below data can make sense.

I have my data in below formate

const sampelData = [{
    'car1 critical':  [1, 1, 1, 1, 1, 1],
    'car1 good':  [1, 1, 1, 1, 1, 1, 1],
    'car1 warning':  [1, 1, 1, 1, 1, 1],
    'car2 critical':  [0, 0, 0, 0, 0,0],
    'car2 good':  [0, 0, 0, 0, 0, 0],
    'car2 warning': [0, 0, 0, 0, 0, 0]
 }]

and i want to manipulate in such a way that array will get assign to new key with in the same key with some more object

 Desired output :-
    [{
            'car1 critical':  {data:[1, 1, 1, 1, 1, 1], condition:"critical"},
            'car1 good':  {data:[1, 1, 1, 1, 1, 1], condition:"good"},
            'car1 warning':  {data:[1, 1, 1, 1, 1, 1], condition:"warning"},
            'car2 critical':  {data:[0, 0, 0, 0, 0, 0], condition:"critical"},
            'car2 good':  {data:[0, 0, 0, 0, 0, 0], condition:"good"},
            'car2 warning': {data:[0, 0, 0, 0, 0, 0], condition:"warning"}
   }]

If there are any other ways of handling such data, any info related to this will be very knowledgeable and helpful.

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

0

You can use Array map() with a for..in loop to itirate thru each object and modify its value. Here is an example:

const sampleData = [{
  'car1 critical': [1, 1, 1, 1, 1, 1],
  'car1 good': [1, 1, 1, 1, 1, 1, 1],
  'car1 warning': [1, 1, 1, 1, 1, 1],
  'car2 critical': [0, 0, 0, 0, 0, 0],
  'car2 good': [0, 0, 0, 0, 0, 0],
  'car2 warning': [0, 0, 0, 0, 0, 0]
}]

// map sampleData
sampleData.map(item => {
  // Iterate object in sampleData
  for (let i in item) {
    // assign new value to each item in object
    item[i] = {
      data: item[i],
      condition: i.split(' ')[1]
    }
  }
})

console.log(sampleData)

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