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