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
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.
You can make convert object to string and count with regular expressions.
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: