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]

If you have a question, put $5 at patreon and message me.

JavaScript in Depth

JS Obj Ref

DOM


JavaScript in Depth

Basic Syntax

Value Types

Variable

String

Function

Object Property

Object and Inheritance

Array

Constructor/Class

Iterable 🌟

Regular Expression

Date

Set Object

Map Object

Number

Misc