Table of contents
1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter and Return Value
3.1.
Syntax: 
3.2.
Parameters:
3.3.
Examples 
3.4.
Identifying Array-Like Objects:
3.5.
JavaScript
3.6.
Using in Function That Handles Collections:
3.7.
Conditionally Iterating Over Elements:
3.8.
JavaScript
3.9.
Filtering Array-Like Items from a Collection:
3.10.
JavaScript
4.
Frequently Asked Questions
4.1.
What is considered an 'array-like' object in JavaScript?
4.2.
How does _.isArrayLike() differ from _.isArray()?
4.3.
Can _.isArrayLike() be used with functions or non-indexed objects?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.isArrayLike() Method

Author Riya Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In JavaScript, handling different types of collections, such as arrays and array-like objects, is a common task. To facilitate this, Lodash provides the _.isArrayLike() method, a utility to check whether a value is array-like. 

Lodash _.isArrayLike() Method

This method is particularly useful in scenarios where you need to work with data structures that behave like arrays but may not be actual arrays, such as arguments objects or NodeList collections.

Why This Function is Used

The _.isArrayLike() function is used to determine if a given value has characteristics of an array, such as having a numeric length property and indexed elements. This method is essential for ensuring that functions designed to work with arrays can also operate on array-like objects. It helps to generalize operations like iteration and indexing, making them applicable to a wider range of data structures.

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

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

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

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 DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass