Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

140
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda