Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error object property can not be iterated by for loop

Tags:

javascript

I often use for loop to iterate JavaScript objects, and have never find a problem until today. It astonished me when I am trying to know what properties an Error object have.

var e= new Error("testing");

for(var key in e)
  console.log(e[key]);

I got nothing, not ouput. But of course, the following code

console.log(e.name);
console.log(e.message);

gives intended output:

"Error"
"testing"

I am not sure what is happening. I tried to Google around but have not find an answer yet. Can some one give me an explanation?

like image 670
Haijin Avatar asked Aug 03 '18 17:08

Haijin


People also ask

Why int object is not iterable in Python?

But the integers are not iterable. As the Var variable holds a single integer value 5, it cannot be iterated using a for loop or any other loop. This is because of the absence of __iter__ method. Which we have discussed about below in example 2. Thus the error “TypeError: int object is not iterable” occurs.

What if % iterator doesn’t exist?

If it doesn’t exist, you’re given the following error. Meaning, %IteratorPrototype has to be used by an object when it’s created so that it can be looped over, and the iterator object of an object, such as [1, 2, 3], is called Symbol.iterator — its internal term in the specification is %%iterator, but you can’t use this name in your code.

Why can't I iterate int objects in Vultr?

Deploy on Vultr with $100 Credit! Int objects are not directly iterable as they hold a single integer value and do not contain the \*\*‘`__iter__`‘ \** method. Instead of using int, try using list if it makes sense, and it can be iterated using for and while loop easily.

Can You iterate over an object in JavaScript?

An iterable can be a built-in iterable type such as Array, String or Map, a generator result, or an object implementing the iterable protocol. In JavaScript, Object s are not iterable unless they implement the iterable protocol. Therefore, you cannot use for…of to iterate over the properties of an object.


1 Answers

JavaScript properties can be enumerable or non-enumerable. Non-enumerable properties are left out of for-in loops and most other ways of getting the property names from the object.

You can get an object's own property names, including non-enumerable ones, via Object.getOwnPropertyNames. So for instance:

Object.getOwnPropertyNames(e).forEach(function(name) {
    console.log(e[name]);
});

or with ES2015+:

for (const name of Object.getOwnPropertyNames(e)) {
    console.log(e[name]);
}

Note that the "own" means that it doesn't include inherited properties, which message may or may not be depending on what JavaScript engine the code is running on. To get all of the object's string-named properties (ignoring Symbol-named ones), you need a loop looking at the prototype. You probably want to stop when you reach Object.prototype. So:

var obj = e;
while (obj !== Object.prototype) {
    Object.getOwnPropertyNames(obj).forEach(name => console.log(obj[name]));
    obj = Object.getPrototypeOf(obj);
}
like image 187
T.J. Crowder Avatar answered Nov 14 '22 21:11

T.J. Crowder