javascript / beginner
Snippet
Counting Object Properties with Object.keys()
Object.keys() returns an array containing all the names (keys) of an object's properties. In Next.js, this is useful for checking if a configuration object is empty or for counting items without iterating manually.
snippet.js
javascript
1
2
3
4
function SettingsSummary(props) {const keys = Object.keys(props.config);return <span>Total settings: ' + keys.length + '</span>;}
nextjs
Breakdown
1
Object.keys(props.config)
This method takes the object and extracts its property names into a standard array.
2
keys.length
Since Object.keys returns an array, we can use .length to find out how many properties the object has.