• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

201
Views
Cómo recorrer una matriz de objetos anidados y eliminar las claves externas, y simplemente dejar el resto del objeto en Nodejs javascript

Soy nuevo en Javascript y Nodejs y no puedo averiguar cómo recorrer la siguiente matriz de objetos y eliminar sus claves numéricas. Entonces, cuando extraigo las claves de cada objeto en la matriz, obtengo: ['City', 'Operator Name', 'Operator Id', 'Transaction Type'] para cada objeto.

Se proporciona una matriz de objetos anidados en este formato.

 [ { '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' } } ]

Necesito las claves de cada objeto: ['City', 'Operator Name', 'Operator Id', 'Transaction Type']

about 3 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Creo que eso es lo que estás tratando de hacer.

 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 3 years ago · Juan Pablo Isaza Report

0

El formato mencionado a continuación es incorrecto aquí e incluso con su corrección, no tiene mucho sentido, pero intentaré ayudar. Lo que puedo entender es que está tratando de extraer los valores de los objetos dados de la matriz aquí, lo que puede hacer es usar el operador Object.values() Leer aquí . Así que podemos hacer dos cosas aquí si solo quieres las claves, puedes hacer Object.keys() que te dará esto

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

Y si desea extraer los valores y mantenerlos en formato de matriz, puede hacer Object.values() que le dará el resultado aquí

 [{ 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, … }]

Y si desea que estos valores se envuelvan en otro Objeto en lugar de Array, simplemente haga esto const obj={...array} Espero que esto ayude, por favor marque la respuesta como correcta si ayuda

about 3 years ago · Juan Pablo Isaza Report

0

Creo que quisiste decir esto: una variedad de objetos. Mire cuidadosamente el suyo, es un tipo de datos no válido, si tiene llaves { } está declarando un objeto que necesita una clave con el valor.

 [ { 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' } ]

En cuanto a la solución

 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 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error