Syntax, Parameter and Return Value
Syntax:
_.isArrayLike(value)
Parameters:
-
value: The value to check.
- Return Value: (boolean) - Returns true if value is array-like, else false.
Examples
Identifying Array-Like Objects:
JavaScript
var _ = require('lodash');
console.log(_.isArrayLike([1, 2, 3]));
console.log(_.isArrayLike(document.querySelectorAll('div')));
console.log(_.isArrayLike('abc'));
console.log(_.isArrayLike({ length: 3 }));
You can also try this code with Online Javascript Compiler
Run Code
Output:
true (actual array)
true (NodeList)
true (string with indexed characters)
true (object with a length property)
Demonstrates how _.isArrayLike() can identify various array-like objects.
Using in Function That Handles Collections:
function processCollection(collection) {
if (_.isArrayLike(collection)) {
// Handle array-like processing
} else {
// Handle other types
}
}
You can also try this code with Online Javascript Compiler
Run Code
Shows using _.isArrayLike() to determine if a function should process a collection as an array-like object.
Conditionally Iterating Over Elements:
JavaScript
var maybeArray = { length: 2, 0: 'a', 1: 'b' };
if (_.isArrayLike(maybeArray)) {
for (var i = 0; i < maybeArray.length; i++) {
console.log(maybeArray[i]);
}
}
You can also try this code with Online Javascript Compiler
Run Code
Output:
'a' and then 'b'
An example of iterating over an array-like object when it passes the _.isArrayLike() check.
Filtering Array-Like Items from a Collection:
JavaScript
var mixedCollection = ['hello', 5, { length: 3 }, [1, 2, 3]];
var arrayLikeItems = _.filter(mixedCollection, _.isArrayLike);
console.log(arrayLikeItems);
You can also try this code with Online Javascript Compiler
Run Code
Output:
['hello', { length: 3 }, [1, 2, 3]]
Demonstrates filtering array-like items from a mixed collection.
Also read about, Queryselectorall
Frequently Asked Questions
What is considered an 'array-like' object in JavaScript?
An 'array-like' object is any object with a numeric length property and indexed elements. Common examples include strings, arguments objects, and NodeList collections.
How does _.isArrayLike() differ from _.isArray()?
_.isArray() checks specifically for arrays, while _.isArrayLike() checks for any object that has array-like characteristics, including actual arrays.
Can _.isArrayLike() be used with functions or non-indexed objects?
No, _.isArrayLike() returns false for functions, even with a length property, and for objects without indexed elements.
Conclusion
Lodash's _.isArrayLike() method provides a versatile way to identify array-like objects, facilitating operations that typically apply to arrays. It's a valuable tool for ensuring compatibility and flexibility when working with various data structures in JavaScript.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.