JS: Array-Like Object

By Xah Lee. Date: . Last updated: .

What is a array-like object?

A object such that:

  1. has property names of consecutive non-negative integers {"0", "1", "2", …}.
  2. has a property "length" with a value that's the number of these properties.
  3. is not a true array. That is, Array.isArray(obj) returns false.

[see JS: Understand JS Array]

What is the difference between array and array-like object?

// create a array-like object
let a1 = {0:"a", 1:"b" , length:2};
a1[2] = 99;
console.log(a1.length) // 2
// length would be 3 if it's a true array

// real array
let a2 = ["a", "b"];
a2[2] = 99;
console.log(a2.length) // 3

[see JS: Prototype and Inheritance]

[see JS: Array.prototype]

How to check if a object is true array?

Array.isArray(obj)

[see JS: Array.isArray]

Where does array-like object came from?

In DOM, method such as document.getElementsByClassName return array-like objects.

JavaScript's arguments object is a array-like object.

[see JS: Function's “arguments” Object]

How to convert array-like object to array?

[see JS: Array-Like Object to Array]

Array Topic

  1. JS: Array Basics
  2. JS: Understand JS Array
  3. JS: Create Array
  4. JS: Sparse Array
  5. JS: Array-Like Object
  6. JS: Array How-To

  1. JS: Array Object
  2. JS: Array.prototype
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth