Gets the size of collections by returning the length for array-like values or the number of own enumerable properties for objects. This is a modularized build of lodash's _.size method, exported as a standalone Node.js module.
npm install lodash.sizevar size = require('lodash.size');For modern environments supporting ES modules:
import size from 'lodash.size';var size = require('lodash.size');
// Array size
size([1, 2, 3]);
// => 3
// Object size (number of own enumerable properties)
size({ 'a': 1, 'b': 2 });
// => 2
// String size
size('pebbles');
// => 7
// Null/undefined handling
size(null);
// => 0
size(undefined);
// => 0
// Empty values
size([]);
// => 0
size({});
// => 0
size('');
// => 0Gets the size of a collection by returning its length for array-like values or the number of own enumerable properties for objects.
/**
* Gets the size of collection by returning its length for array-like
* values or the number of own enumerable properties for objects.
*
* @param {Array|Object|string} collection The collection to inspect.
* @returns {number} Returns the size of collection.
*/
function size(collection)Parameters:
collection (Array|Object|string): The collection to inspectReturns:
Behavior:
length property): returns the length valuelodash.keys internally)null or undefined: returns 0Examples:
// Arrays
size([1, 2, 3, 4, 5]);
// => 5
// Array-like objects
size({ 0: 'a', 1: 'b', 2: 'c', length: 3 });
// => 3
// Plain objects
size({ foo: 'bar', baz: 'qux' });
// => 2
// Strings
size('hello world');
// => 11
// Edge cases
size(null); // => 0
size(undefined); // => 0
size([]); // => 0
size({}); // => 0
size(''); // => 0
// Objects with length property that isn't valid
size({ length: 'invalid', a: 1, b: 2 });
// => 2 (counts properties, ignores invalid length)