Home > Back-end >  How to loop through an array of nested object and remove the outer keys, and just leave the rest of
How to loop through an array of nested object and remove the outer keys, and just leave the rest of

Time:04-17

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

CodePudding user response:

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)

CodePudding user response:

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

CodePudding user response:

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

  • Related