8
rated 0 times
[
12]
[
4]
/ answers: 1 / hits: 33505
/ 6 Years ago, tue, july 17, 2018, 12:00:00
My understanding is that for...in
loops are designed to iterate over objects in Javascript. See this post and this post.
Take the following example. This returns 'Uncaught TypeError: items is not iterable' in my console.
var text = {
name: Coptic,
ranges: [[994, 1008], [11392, 11508], [11513, 11520]],
direction: ltr,
year: -200,
living: false,
link: https://en.wikipedia.org/wiki/Coptic_alphabet
};
function dominantDirection(items) {
for (let item of items) {
if (item.direction === 'ltr') {
return 'ltr';
} else {
return 'rtl';
}
}
}
console.log(dominantDirection(text));
If I wrap the object in an array[] it works fine.
However my second example works as expected.
var object1 = {a: 1, b: 2, c: 3};
var string1 = ;
function loopObj() {
for (var property1 in object1) {
console.log(string1 = string1 + object1[property1]);
}
}
console.log(loopObj());
Why does the first example require an array and the second does not?
More From » object