Syntax, Parameter and Return Value
Syntax:
_.isArrayLikeObject(value)
Parameters:
value: The value to check.
Return Value:
(boolean) - Returns true if value is an object and array-like, else false.
Examples
Identifying Array-Like Objects:
JavaScript
var _ = require('lodash');
console.log(_.isArrayLikeObject([1, 2, 3]));
console.log(_.isArrayLikeObject('abc'));
console.log(_.isArrayLikeObject({ length: 3, 0: 'a', 1: 'b' }));
You can also try this code with Online Javascript Compiler
Run Code
Output:
true (actual array)
false (string is not an object)
true (object with array-like properties)
Demonstrates how _.isArrayLikeObject() differentiates between array-like objects and other types.
Usage in Data Processing Functions:
function processData(data) {
if (_.isArrayLikeObject(data)) {
// Handle processing for array-like objects
} else {
// Handle other data types
}
}
You can also try this code with Online Javascript Compiler
Run Code
Shows using _.isArrayLikeObject() to conditionally process data based on its type.
Iterating Over Elements in Array-Like Objects:
JavaScript
var collection = { length: 2, 0: 'first', 1: 'second' };
if (_.isArrayLikeObject(collection)) {
for (var i = 0; i < collection.length; i++) {
console.log(collection[i]);
}
You can also try this code with Online Javascript Compiler
Run Code
}
Output:
'first' and 'second'
An example of iterating over an object that is array-like.
Filtering Array-Like Objects from a Collection:
JavaScript
var mixedCollection = ['hello', { length: 3, 0: 'a', 1: 'b' }, [1, 2, 3]];
var arrayLikeObjects = _.filter(mixedCollection, _.isArrayLikeObject);
console.log(arrayLikeObjects);
You can also try this code with Online Javascript Compiler
Run Code
Output:
[{ length: 3, 0: 'a', 1: 'b' }, [1, 2, 3]]
Demonstrates filtering array-like objects from a collection that includes various types.
Frequently Asked Questions
How does _.isArrayLikeObject() differ from _.isArrayLike()?
While _.isArrayLike() checks for array-like properties regardless of the data type, _.isArrayLikeObject() specifically checks for values that are both object-like and array-like, excluding non-object types like strings.
What are common use cases for _.isArrayLikeObject()?
Common use cases include iterating over collections, applying array methods to objects with numeric indexes, and general data validation where array-like structure is expected.
Can functions be considered array-like objects?
Typically, functions are not considered array-like objects, even if they have indexed properties and a length attribute, because they are a different type in JavaScript.
Conclusion
Lodash's _.isArrayLikeObject() method is an effective utility for identifying values that possess both the characteristics of an object and the traits of an array-like structure. It is particularly useful in scenarios where such a specific type check is crucial for correct data handling and manipulation.
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.