Home > database >  How to get the list of all keys from this array of object?
How to get the list of all keys from this array of object?

Time:07-26

I want to get a list of array of object, which should give me all the keys for the array.

The list should return names of all keys that is : [districtId, districtName, verified, unVerified, verificationPer, enumeratorPer, totalRegistered ].

The array of object is here :

 let a =   [{
  "districtId": "2351",
  "districtName": "A",
  "verified": 0,
  "unVerified": 0,
  "verificationPer": 0,
  "enumeratorPer": 0,
  "totalRegistered": 2
}, {
  "districtId": "2349",
  "districtName": "A",
  "verified": 1,
  "unVerified": 0,
  "verificationPer": 100,
  "enumeratorPer": 0,
  "totalRegistered": 1
}, {
  "districtId": "2332",
  "districtName": "B",
  "verified": 1,
  "unVerified": 0,
  "verificationPer": 0,
  "enumeratorPer": 0,
  "totalRegistered": 4
}, {
  "districtId": "2343",
  "districtName": "C",
  "verified": 0,
  "unVerified": 0,
  "verificationPer": 0,
  "enumeratorPer": 0,
  "totalRegistered": 1
}, {
  "districtId": "2302",
  "districtName": "M",
  "verified": 0,
  "unVerified": 0,
  "verificationPer": 0,
  "enumeratorPer": 0,
  "totalRegistered": 2
}, {
  "districtId": "2301",
  "districtName": "S",
  "verified": 0,
  "unVerified": 0,
  "verificationPer": 0,
  "enumeratorPer": 0,
  "totalRegistered": 1
}]

CodePudding user response:

If all the objects in the array have the same set of keys then you can use Object.keys(a[0]) else you can iterate through the array and run Object.keys(a[n]) where n is each iteration.

let a =   [{
  "districtId": "2351",
  "districtName": "A",
  "verified": 0,
  "unVerified": 0,
  "verificationPer": 0,
  "enumeratorPer": 0,
  "totalRegistered": 2
}, {
  "districtId": "2349",
  "districtName": "A",
  "verified": 1,
  "unVerified": 0,
  "verificationPer": 100,
  "enumeratorPer": 0,
  "totalRegistered": 1
}, {
  "districtId": "2332",
  "districtName": "B",
  "verified": 1,
  "unVerified": 0,
  "verificationPer": 0,
  "enumeratorPer": 0,
  "totalRegistered": 4
}, {
  "districtId": "2343",
  "districtName": "C",
  "verified": 0,
  "unVerified": 0,
  "verificationPer": 0,
  "enumeratorPer": 0,
  "totalRegistered": 1
}, {
  "districtId": "2302",
  "districtName": "M",
  "verified": 0,
  "unVerified": 0,
  "verificationPer": 0,
  "enumeratorPer": 0,
  "totalRegistered": 2
}, {
  "districtId": "2301",
  "districtName": "S",
  "verified": 0,
  "unVerified": 0,
  "verificationPer": 0,
  "enumeratorPer": 0,
  "totalRegistered": 1
}]

console.log(Object.keys(a[0]));

CodePudding user response:

You can use Object.keys() in order to get all keys of one of the items in your array.

Object.keys(a[0]));

Full working Example:

let a =   [{
  "districtId": "2351",
  "districtName": "A",
  "verified": 0,
  "unVerified": 0,
  "verificationPer": 0,
  "enumeratorPer": 0,
  "totalRegistered": 2
}, {
  "districtId": "2349",
  "districtName": "A",
  "verified": 1,
  "unVerified": 0,
  "verificationPer": 100,
  "enumeratorPer": 0,
  "totalRegistered": 1
}, {
  "districtId": "2332",
  "districtName": "B",
  "verified": 1,
  "unVerified": 0,
  "verificationPer": 0,
  "enumeratorPer": 0,
  "totalRegistered": 4
}, {
  "districtId": "2343",
  "districtName": "C",
  "verified": 0,
  "unVerified": 0,
  "verificationPer": 0,
  "enumeratorPer": 0,
  "totalRegistered": 1
}, {
  "districtId": "2302",
  "districtName": "M",
  "verified": 0,
  "unVerified": 0,
  "verificationPer": 0,
  "enumeratorPer": 0,
  "totalRegistered": 2
}, {
  "districtId": "2301",
  "districtName": "S",
  "verified": 0,
  "unVerified": 0,
  "verificationPer": 0,
  "enumeratorPer": 0,
  "totalRegistered": 1
}]

console.log(Object.keys(a[0]));

CodePudding user response:

function onlyUnique(value, index, self) {
  return self.indexOf(value) === index;
}
const b = a.flatMap(item => Object.keys(item)).filter(onlyUnique)

or simply use code below if you are sure that all the keys of objects are the same

Object.keys(a[0]);
  • Related