Array.entries() is not working on Object

const arrayLike={
  a:1,
  b:2,
  c:"lion"
}
for(const entry of Array.prototype.entries.call(arrayLike)){
  console.log(entry);
}

Is there any mistake in the above object declaration?
I am not getting any o\p, Can any one help me in resolving this issue?

Hi @vaishnavi_A.N

For an object to be “array-like” two conditions need to be met.
From “Notes” in the “Array” article:

Array-like objects
The term array-like object refers to any object that doesn’t throw during the length conversion process described above. In practice, such object is expected to actually have a length property and to have indexed elements in the range 0 to length - 1 . (If it doesn’t have all indices, it will be functionally equivalent to a sparse array.)

You need a length property and the keys need to be numbers in the correct range.

Michael

1 Like

So keys should only be numbers
Not even strings?
Right?
Sorry that I am asking without doing practice as I am out of station.
Due to the quest I am asking you.

For array-like objects, yes (and a “length” key).

1 Like