size()
size<
T>(collection):number
size<
T>(collection):number
Gets the size of collection by returning its length for array-like values or the number of own enumerable string keyed properties for objects.
DEPRECATED
Use array.length or Object.keys().length directly instead.
Type Parametersβ
T: Tβ
The type of elements in the array, or the object type.
Parametersβ
Overload 1:
collection: T[]β
The collection to inspect.
Overload 2:
collection: Tβ
The collection to inspect.
Returns: numberβ
The collection size.
See Alsoβ
Sinceβ
2.0.0
Also known asβ
length (Ramda, Effect) Β· size (Lodash, es-toolkit) Β· β (Remeda, Radashi, Modern Dash, Antfu)
Exampleβ
const numbers = [1, 2, 3, 4, 5];
// β Deprecated approach
const arraySize = size(numbers);
console.log(arraySize); // 5
// β
Recommended approach (for arrays)
const arraySizeNative = numbers.length;
console.log(arraySizeNative); // 5
// β
Recommended approach (for objects)
const obj = { a: 1, b: 2, c: 3 };
const objSizeNative = Object.keys(obj).length;
console.log(objSizeNative); // 3
How it works?β
Gets the length of collection.
Deprecated: Use .length or .size directly.
Native Equivalentβ
// β size(arr)
// β
arr.length
// β size(map)
// β
map.size
Use Casesβ
Get collection length πβ
Get the number of elements in a collection.
const items = [1, 2, 3, 4, 5];
items.length;
// => 5
Count object propertiesβ
Get the number of properties in an object.
const config = { host: "localhost", port: 3000, debug: true };
Object.keys(config).length;
// => 3
Check if emptyβ
Determine if a collection is empty.
const results = [];
results.length === 0;
// => true