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

257
Visualizações
How to loop through an array of nested object and remove the outer keys, and just leave the rest of the object in Nodejs javascript

I am new to Javascript and Nodejs and I can't figure out how to loop through the below array of objects and remove its numerical keys. So that when I extract the keys of each object in the array I get - ['City', 'Operator Name', 'Operator Id', 'Transaction Type'] for each object.

An array of nested objects is given in this format

[
  {
    '0': {
      City: 'Kottayam',
      'Operator Name': 'GEORGE KUTTY K',
      'Operator Id': 1808,
      Amount: 3200,
      'Transaction Type': 'Received'
    },
    '1': {
      City: 'Kottayam',
      'Operator Name': 'SAJI VARGHESE',
      'Operator Id': 1723,
      Amount: 276.512,
      'Transaction Type': 'Received'
    },
    '2': {
      City: 'Kottayam',
      'Operator Name': 'SUNEESH K D',
      'Operator Id': 1010,
      Amount: 600,
      'Transaction Type': 'Received'
    },
    '3': {
      City: 'Kottayam',
      'Operator Name': 'PRASAD TK',
      'Operator Id': 1745,
      Amount: 19.8,
      'Transaction Type': 'Paid'
    }
  }
]

I need the keys of each object - ['City', 'Operator Name', 'Operator Id', 'Transaction Type']

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

I think that's what you're trying to do

let oldArr = [
  {
    '0': {
      City: 'Kottayam',
      'Operator Name': 'GEORGE KUTTY K',
      'Operator Id': 1808,
      Amount: 3200,
      'Transaction Type': 'Received'
      }
  },
    {'1': {
      City: 'Kottayam',
      'Operator Name': 'SAJI VARGHESE',
      'Operator Id': 1723,
      Amount: 276.512,
      'Transaction Type': 'Received'
      }
   },
    {'2': {
      City: 'Kottayam',
      'Operator Name': 'SUNEESH K D',
      'Operator Id': 1010,
      Amount: 600,
      'Transaction Type': 'Received'
      }
    },
    {'3': {
      City: 'Kottayam',
      'Operator Name': 'PRASAD TK',
      'Operator Id': 1745,
      Amount: 19.8,
      'Transaction Type': 'Paid'
    }
  }
]
let newArr = [];

oldArr.forEach(obj => newArr.push(...Object.values(obj)))

console.log(newArr)

about 4 years ago · Juan Pablo Isaza Relatório

0

The format mentioned below is wrong here and even with your correction, doesn't make sense that much but I'll try to help. What I can understand is you are trying to extract the values of the given objects the the array here, what you can do is use Object.values() operator Read Here. So we can do two things here if you just want the keys you can do Object.keys() which will give you this

Array(4) [ "0", "1", "2", "3" ]

And if you want to extract the values and keep it into Array format you can do Object.values() which will give you the output here

[{ City: "Kottayam", "Operator Name": "GEORGE KUTTY K", "Operator Id": 1808, … },
​{ City: "Kottayam", "Operator Name": "SAJI VARGHESE", "Operator Id": 1723, … },
{ City: "Kottayam", "Operator Name": "SUNEESH K D", "Operator Id": 1010, … },
{ City: "Kottayam", "Operator Name": "PRASAD TK", "Operator Id": 1745, … }]

And if you want these values wrapped around in another Object instead of Array just do this const obj={...array} I hope this helps, please uptick and mark the answer as correct if it helps

about 4 years ago · Juan Pablo Isaza Relatório

0

I think you meant this - an array of objects. Look carefully at yours, it's an invalid data type, if you have braces { } you're declaring an object that needs a key with the value.

[
    {
      City: 'Kottayam',
      'Operator Name': 'GEORGE KUTTY K',
      'Operator Id': 1808,
      Amount: 3200,
      'Transaction Type': 'Received'
    },
    {
      City: 'Kottayam',
      'Operator Name': 'SAJI VARGHESE',
      'Operator Id': 1723,
      Amount: 276.512,
      'Transaction Type': 'Received'
    },
    {
      City: 'Kottayam',
      'Operator Name': 'SUNEESH K D',
      'Operator Id': 1010,
      Amount: 600,
      'Transaction Type': 'Received'
    },
    {
      City: 'Kottayam',
      'Operator Name': 'PRASAD TK',
      'Operator Id': 1745,
      Amount: 19.8,
      'Transaction Type': 'Paid'
    }
]

As for the solution

let array =
[
  {
    '0': {
      City: 'Kottayam',
      'Operator Name': 'GEORGE KUTTY K',
      'Operator Id': 1808,
      Amount: 3200,
      'Transaction Type': 'Received'
    },
    '1': {
      City: 'Kottayam',
      'Operator Name': 'SAJI VARGHESE',
      'Operator Id': 1723,
      Amount: 276.512,
      'Transaction Type': 'Received'
    },
    '2': {
      City: 'Kottayam',
      'Operator Name': 'SUNEESH K D',
      'Operator Id': 1010,
      Amount: 600,
      'Transaction Type': 'Received'
    },
    '3': {
      City: 'Kottayam',
      'Operator Name': 'PRASAD TK',
      'Operator Id': 1745,
      Amount: 19.8,
      'Transaction Type': 'Paid'
    }
  }
]

console.log(array); //this is your starting array, notice it just has one object in there at index 0

let arrayTransform = []; //this is where we will store our result

for (const [key, value] of Object.entries(array[0])){
  console.log(key,value);
  arrayTransform.push(value);
}

console.log('array transform looks like', arrayTransform);

//arrayTransform should look like this
//[
//   {
//     City: 'Kottayam',
//     'Operator Name': 'GEORGE KUTTY K',
//     'Operator Id': 1808,
//     Amount: 3200,
//     'Transaction Type': 'Received'
//   },
//   {
//     City: 'Kottayam',
//     'Operator Name': 'SAJI VARGHESE',
//     'Operator Id': 1723,
//     Amount: 276.512,
//     'Transaction Type': 'Received'
//   },
//   {
//     City: 'Kottayam',
//     'Operator Name': 'SUNEESH K D',
//     'Operator Id': 1010,
//     Amount: 600,
//     'Transaction Type': 'Received'
//   },
//   {
//     City: 'Kottayam',
//     'Operator Name': 'PRASAD TK',
//     'Operator Id': 1745,
//     Amount: 19.8,
//     'Transaction Type': 'Paid'
//   }
// ]

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