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

262
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 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