skip to Main Content

How i can count all objects that have specific key but if object can be nested and I do not know max depth of object because everytime is differente?

for exmaple once can be like this:

{
    "coins_series": [
        {
            "series": 1,
            "coins": [
                {
                    "_id": "65c8c78845d911984d98f6be",
                    "currency": "EUR",
                    "reverse_id": "SVN200701001REG",
                    "obverse_id": [
                        "EUR199901001FRO"
                    ],
                    "country": "SVN",
                    "commemorative": false,
                    "series": 1,
                    "min_year": 2007,
                    "denomination": 0.01,
                    "diameter": 16.25,
                    "thickness": 1.67,
                    "weight": 2.3
                },
                {
                    "_id": "65c8c78845d911984d98f6bf",
                    "currency": "EUR",
                    "reverse_id": "SVN200701002REG",
                    "obverse_id": [
                        "EUR199901002FRO"
                    ],
                    "country": "SVN",
                    "commemorative": false,
                    "series": 1,
                    "min_year": 2007,
                    "denomination": 0.02,
                    "diameter": 18.75,
                    "thickness": 1.67,
                    "weight": 3.06
                },
                {
                    "_id": "65c8c78845d911984d98f6c0",
                    "currency": "EUR",
                    "reverse_id": "SVN200701005REG",
                    "obverse_id": [
                        "EUR199901005FRO"
                    ],
                    "country": "SVN",
                    "commemorative": false,
                    "series": 1,
                    "min_year": 2007,
                    "denomination": 0.05,
                    "diameter": 21.25,
                    "thickness": 1.67,
                    "weight": 3.92
                },
                {
                    "_id": "65c8c78845d911984d98f6c1",
                    "currency": "EUR",
                    "reverse_id": "SVN200701010REG",
                    "obverse_id": [
                        "EUR200702010FRO"
                    ],
                    "country": "SVN",
                    "commemorative": false,
                    "series": 1,
                    "min_year": 2007,
                    "denomination": 0.1,
                    "diameter": 19.75,
                    "thickness": 1.93,
                    "weight": 4.1
                },
                {
                    "_id": "65c8c78845d911984d98f6c2",
                    "currency": "EUR",
                    "reverse_id": "SVN200701020REG",
                    "obverse_id": [
                        "EUR200702020FRO"
                    ],
                    "country": "SVN",
                    "commemorative": false,
                    "series": 1,
                    "min_year": 2007,
                    "denomination": 0.2,
                    "diameter": 22.25,
                    "thickness": 2.14,
                    "weight": 5.74
                },
                {
                    "_id": "65c8c78845d911984d98f6c3",
                    "currency": "EUR",
                    "reverse_id": "SVN200701050REG",
                    "obverse_id": [
                        "EUR200702050FRO"
                    ],
                    "country": "SVN",
                    "commemorative": false,
                    "series": 1,
                    "min_year": 2007,
                    "denomination": 0.5,
                    "diameter": 24.25,
                    "thickness": 2.38,
                    "weight": 7.8
                },
                {
                    "_id": "65c8c78845d911984d98f6c4",
                    "currency": "EUR",
                    "reverse_id": "SVN200701100REG",
                    "obverse_id": [
                        "EUR200702100FRO"
                    ],
                    "country": "SVN",
                    "commemorative": false,
                    "series": 1,
                    "min_year": 2007,
                    "denomination": 1,
                    "diameter": 23.25,
                    "thickness": 2.33,
                    "weight": 7.5
                },
                {
                    "_id": "65c8c78845d911984d98f6c5",
                    "currency": "EUR",
                    "reverse_id": "SVN200701200REG",
                    "obverse_id": [
                        "EUR200702200FRO"
                    ],
                    "country": "SVN",
                    "commemorative": false,
                    "series": 1,
                    "min_year": 2007,
                    "denomination": 2,
                    "diameter": 25.75,
                    "thickness": 2.2,
                    "weight": 8.5
                }
            ]
        }
    ],
    "country": "SVN"
}

and other time can be like this:

{
    "series": 2,
    "coins": [
        {
            "_id": "65c8c78845d911984d98f724",
            "currency": "EUR",
            "reverse_id": "VAT200502001REG",
            "obverse_id": [
                "EUR199901001FRO"
            ],
            "country": "VAT",
            "commemorative": false,
            "series": 2,
            "min_year": 2005,
            "max_year": 2005,
            "denomination": 0.01,
            "diameter": 16.25,
            "thickness": 1.67,
            "weight": 2.3
        },
        {
            "_id": "65c8c78845d911984d98f725",
            "currency": "EUR",
            "reverse_id": "VAT200502002REG",
            "obverse_id": [
                "EUR199901002FRO"
            ],
            "country": "VAT",
            "commemorative": false,
            "series": 2,
            "min_year": 2005,
            "max_year": 2005,
            "denomination": 0.02,
            "diameter": 18.75,
            "thickness": 1.67,
            "weight": 3.06
        },
        {
            "_id": "65c8c78845d911984d98f726",
            "currency": "EUR",
            "reverse_id": "VAT200502005REG",
            "obverse_id": [
                "EUR199901005FRO"
            ],
            "country": "VAT",
            "commemorative": false,
            "series": 2,
            "min_year": 2005,
            "max_year": 2005,
            "denomination": 0.05,
            "diameter": 21.25,
            "thickness": 1.67,
            "weight": 3.92
        },
        {
            "_id": "65c8c78845d911984d98f727",
            "currency": "EUR",
            "reverse_id": "VAT200502010REG",
            "obverse_id": [
                "EUR199901010FRO"
            ],
            "country": "VAT",
            "commemorative": false,
            "series": 2,
            "min_year": 2005,
            "max_year": 2005,
            "denomination": 0.1,
            "diameter": 19.75,
            "thickness": 1.93,
            "weight": 4.1
        },
        {
            "_id": "65c8c78845d911984d98f728",
            "currency": "EUR",
            "reverse_id": "VAT200502020REG",
            "obverse_id": [
                "EUR199901020FRO"
            ],
            "country": "VAT",
            "commemorative": false,
            "series": 2,
            "min_year": 2005,
            "max_year": 2005,
            "denomination": 0.2,
            "diameter": 22.25,
            "thickness": 2.14,
            "weight": 5.74
        },
        {
            "_id": "65c8c78845d911984d98f729",
            "currency": "EUR",
            "reverse_id": "VAT200502050REG",
            "obverse_id": [
                "EUR199901050FRO"
            ],
            "country": "VAT",
            "commemorative": false,
            "series": 2,
            "min_year": 2005,
            "max_year": 2005,
            "denomination": 0.5,
            "diameter": 24.25,
            "thickness": 2.38,
            "weight": 7.8
        },
        {
            "_id": "65c8c78845d911984d98f72a",
            "currency": "EUR",
            "reverse_id": "VAT200502100REG",
            "obverse_id": [
                "EUR199901100FRO"
            ],
            "country": "VAT",
            "commemorative": false,
            "series": 2,
            "min_year": 2005,
            "max_year": 2005,
            "denomination": 1,
            "diameter": 23.25,
            "thickness": 2.33,
            "weight": 7.5
        },
        {
            "_id": "65c8c78845d911984d98f72b",
            "currency": "EUR",
            "reverse_id": "VAT200502200REG",
            "obverse_id": [
                "EUR199901200FRO"
            ],
            "country": "VAT",
            "commemorative": false,
            "series": 2,
            "min_year": 2005,
            "max_year": 2005,
            "denomination": 2,
            "diameter": 25.75,
            "thickness": 2.2,
            "weight": 8.5
        }
    ]
}

some other time can be different format. I need to count all objects with commemorative key each time without specific data structure.

3

Answers


  1. let totalCount=0;
    const countFinder = (obj,key ='commemorative',val=undefined) =>{
      if(Array.isArray(obj)){
        for(let item of obj){
          countFinder(item,key,val);
        }
      }
      else if(typeof obj === 'object'){
        if(Object.prototype.hasOwnProperty.call(obj,key)){
          if(val!==undefined){
            if(obj[key] === val){
              totalCount++;
              return;
            }
            return
          }
          else{
            totalCount++;
            return;
          }
        }
        else{
          for(let _key in obj){
            let item = obj[_key];
            countFinder(item,key,val);
          }
        }
      }
      else{
        return;
      }
      return totalCount
    }
    

    This function returns the count of the specified key within the given object or array. For example, when calling countFinder(obj, ‘commemorative’), it will iterate through all elements in the first object to search for the ‘commemorative’ key. If called as countFinder(obj, ‘commemorative’, true), it will count the occurrences of the ‘commemorative’ key with a value of true. Don’t forget to reset totalCount before calling the function.

    Login or Signup to reply.
  2. You can make convert object to string and count with regular expressions.

    JSON.stringify(coinSeriesObject).match(/commemorative/gi).length 
    
    Login or Signup to reply.
  3. You can do this is several ways. Here is an approach using the reduce callback to make the recursive call and add up all the counts:

    const countKey = (obj, key) =>
        Object(obj) !== obj ? 0  // A primitive value (base case)
           // A boolean indicating a match is coerced to 0 or 1:
        : (!Array.isArray(obj) && Object.hasOwn(obj, key)) +  
            // Recursive calls
            Object.values(obj).reduce((sum, obj) => sum + countKey(obj, key), 0);
    
    // Example
    const data = {"coins_series": [{"series": 1,"coins": [{"_id": "65c8c78845d911984d98f6be","currency": "EUR","reverse_id": "SVN200701001REG","obverse_id": ["EUR199901001FRO"],"country": "SVN","commemorative": false,"series": 1,"min_year": 2007,"denomination": 0.01,"diameter": 16.25,"thickness": 1.67,"weight": 2.3},{"_id": "65c8c78845d911984d98f6bf","currency": "EUR","reverse_id": "SVN200701002REG","obverse_id": ["EUR199901002FRO"],"country": "SVN","commemorative": false,"series": 1,"min_year": 2007,"denomination": 0.02,"diameter": 18.75,"thickness": 1.67,"weight": 3.06},{"_id": "65c8c78845d911984d98f6c0","currency": "EUR","reverse_id": "SVN200701005REG","obverse_id": ["EUR199901005FRO"],"country": "SVN","commemorative": false,"series": 1,"min_year": 2007,"denomination": 0.05,"diameter": 21.25,"thickness": 1.67,"weight": 3.92},{"_id": "65c8c78845d911984d98f6c1","currency": "EUR","reverse_id": "SVN200701010REG","obverse_id": ["EUR200702010FRO"],"country": "SVN","commemorative": false,"series": 1,"min_year": 2007,"denomination": 0.1,"diameter": 19.75,"thickness": 1.93,"weight": 4.1},{"_id": "65c8c78845d911984d98f6c2","currency": "EUR","reverse_id": "SVN200701020REG","obverse_id": ["EUR200702020FRO"],"country": "SVN","commemorative": false,"series": 1,"min_year": 2007,"denomination": 0.2,"diameter": 22.25,"thickness": 2.14,"weight": 5.74},{"_id": "65c8c78845d911984d98f6c3","currency": "EUR","reverse_id": "SVN200701050REG","obverse_id": ["EUR200702050FRO"],"country": "SVN","commemorative": false,"series": 1,"min_year": 2007,"denomination": 0.5,"diameter": 24.25,"thickness": 2.38,"weight": 7.8},{"_id": "65c8c78845d911984d98f6c4","currency": "EUR","reverse_id": "SVN200701100REG","obverse_id": ["EUR200702100FRO"],"country": "SVN","commemorative": false,"series": 1,"min_year": 2007,"denomination": 1,"diameter": 23.25,"thickness": 2.33,"weight": 7.5},{"_id": "65c8c78845d911984d98f6c5","currency": "EUR","reverse_id": "SVN200701200REG","obverse_id": ["EUR200702200FRO"],"country": "SVN","commemorative": false,"series": 1,"min_year": 2007,"denomination": 2,"diameter": 25.75,"thickness": 2.2,"weight": 8.5}]}],"country": "SVN"};
    const count = countKey(data, "commemorative");
    console.log(count); // 8
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search