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']
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)
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
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' // } // ]