0

Using this data, how do I find the count of keys for the Customer object and all child element objects?

const Customer = {
"Profile": {
    "A1": {
        "A2": "",
        "A3": "E",
        "A4": "",
        "A5": {
            "Cus": "",
            "Bil": ""
        },
        "Services": [{
            "vA": {
                "Status": 2,
                "Switch-CLLi": "",
                "PLIST": [{
                    "Status": "",
                    "Price": 0
                }, {
                    "Status": "",
                    "Price": 40
                }]
            }
        }]
    },
    "B1": {
        "B2": 953108717,
        "B3": "04"
    }
}} 
Itchydon
  • 2,293
  • 5
  • 16
  • 30
Aleti av
  • 11
  • 5
  • Result should be count of the available keys . – Aleti av Sep 16 '20 at 07:47
  • Hey, Can you edit the question to be in correct format, current JSON is not readable. Also what things have you tried and what is not working ? – Yash Joshi Sep 16 '20 at 07:47
  • const Customer = { "Profile" : {"A1" : {"A2" : "", "A3" : "E","A4" : "","A5" : {"Cus" : "", "Bil" : "" }, "Services" : [ { "vA" : {"Status" : 2, "Switch-CLLi" : "", "PLIST" : [ { "Status" : "", "Price" : 0 }, { "Status" : "", "Price" : 40 } ] } } }, "B1":{ "B2" : 953108717, "B3" : "04" } } } – Aleti av Sep 16 '20 at 07:51
  • This is the data i have and i need the total count of keys present, including with nested objects keys i had tried --> console.log(Object.keys(obj1.Profile).length) Here obj1 is the reference of Customer variable. – Aleti av Sep 16 '20 at 07:55
  • Does this answer your question: https://stackoverflow.com/questions/126100/how-to-efficiently-count-the-number-of-keys-properties-of-an-object-in-javascrip?rq=1 – Lance Sep 16 '20 at 07:56

2 Answers2

0

you can user recursion for this.

const countKeys = (obj, result) => {
    const keys = Object.keys(obj);
   result.sum+= keys.length;
   result.keys = [...result.keys, ...keys];
   keys.forEach(key => {
      if(typeof(obj[key]) === 'object')
       result= countKeys(obj[key],result);
   })
   return result;
};

call this method in you method

const totalKeys = countKeys(yourObject, { sum: 0, keys: []});
Sujit.Warrier
  • 2,337
  • 2
  • 25
  • 38
  • It works!!! Thanks alot Sujit.And one more how can we get all the keys names from these – Aleti av Sep 16 '20 at 10:49
  • updated. Please mark as answer if this solves your problem – Sujit.Warrier Sep 16 '20 at 13:28
  • It is first looping to all the main keys and then it's taking the nested keys, can we have a sequential order of the keys like when reading, if a nested object is found then it should also print the nested object keys along with the main key names . Eg:- Suppose from the json sample A5 key has nested object(Cus and Bil) then it should go into the netetd object print the keys and then next it will go to other key called Services. – Aleti av Sep 16 '20 at 13:54
  • Modify the function to do that. I have given you the basic approach – Sujit.Warrier Sep 16 '20 at 15:10
  • Stackoverflow is used to get the basic approach to solve your problem. use it to create your own logic. – Sujit.Warrier Sep 17 '20 at 04:31
0

Try not to reinvent the wheel and find a good library for data processing and learn that. We use object-scan for almost everything. It's pretty powerful once you wrap your head around it. Here is how you could solve your questions

// const objectScan = require('object-scan');

const count = (data) => objectScan(['**'], {
  filterFn: ({ property, context }) => {
    if (typeof property === 'string') {
      context[property] = (context[property] || 0) + 1;
    }
  }
})(data, {});

const Customer = { Profile: { A1: { A2: '', A3: 'E', A4: '', A5: { Cus: '', Bil: '' }, Services: [{ vA: { Status: 2, 'Switch-CLLi': '', PLIST: [{ Status: '', Price: 0 }, { Status: '', Price: 40 }] } }] }, B1: { B2: 953108717, B3: '04' } } };

console.log(count(Customer));
// => { B3: 1, B2: 1, B1: 1, Price: 2, Status: 3, PLIST: 1, 'Switch-CLLi': 1, vA: 1, Services: 1, Bil: 1, Cus: 1, A5: 1, A4: 1, A3: 1, A2: 1, A1: 1, Profile: 1 }
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://bundle.run/object-scan@13.8.0"></script>

Disclaimer: I'm the author of object-scan

vincent
  • 1,290
  • 1
  • 11
  • 19