Hi guys iam trying to find the count of a specified field if its present inside a object of array
For example
[{name:'Linus',rate:'23'},{name:'Sebastin'},{name:'Alex',rate:''}]
what iam trying to achieve is count the number of key ‘rate’ inside the object
iam expecting a output like
2
>Solution :
You can use reduce to iterate the input data and count the number of times the key occurs in one of the objects:
data = [{name:'Linus',rate:'23'},{name:'Sebastin'},{name:'Alex',rate:''}]
const countKeys = (data, key) => data.reduce((acc, o) => acc + (key in o), 0)
console.log(countKeys(data, 'rate'))