JS: Array-Like Object
What is a array-like object
A object such that is all of the following:
Array.isArray(obj)
returnsfalse
.- Has property names of consecutive non-negative string integers {
"0"
,"1"
,"2"
, etc}. - Has a property
"length"
with a value that's the count of these properties, and typically it has the Enumerable attribute false.
Create a Array-Like Object
// create a array-like object let xx = { 0: "a", 1: "b", length: 2 }; Reflect.defineProperty(xx, "length", { enumerable: false });
Difference Between Array vs Array-Like Object
Check if a object is true array
Where does array-like object came from
- In DOM, method such as
document.getElementsByClassName
return array-like objects. γsee Get Element by ID, Name, Class etcγ - function arguments Object is a array-like object.