Reputation: 2719
I am trying to print array element to console using for each loop in javascript. But I am printing something which I am not able to figure out what!
let arr=["1,2,3","iosajah","undefined"];
for(let data in arr)
{
console.log(data);// prints 4,0,1,2
if(typeof data === "undefined")
{
//do something
}
}
console.log(data) prints 4,0,1,2
but I expected it to print each array element
Upvotes: 3
Views: 21645
Reputation: 5432
I have changed data
to key
since that is what the for... in...
syntax produces: the key of the object (which may be an Array) during the iteration. You should not have printed a 4
at all as this typescript playground shows: Typescript Playground Example
let arr=["1,2,3","iosajah","undefined"];
for(let key in arr)
{
console.log(arr[key]);// prints 0,1,2
if(typeof arr[key] === "undefined")
{
//do something
}
}
If you want to iterate over an array the more typical way to do it is using the .forEach
syntax.
let arr=["1,2,3","iosajah","undefined"];
arr.forEach((element, index, arr) => {
console.log(element, index);
if (typeof element === "undefined") {
//do something
}
})
It also looks like you probably are intending to look for the string undefined
in the array and if that is the case then you don't want to check the typeof
, you just want to compare the string value:
if (element === "undefined") {
}
The typeof element
will never be a string of value "undefined"
in this example.
Upvotes: 1
Reputation: 386560
You could take a for ... of
statement where you get the element instead of the for ... in
statement where you get the indices of the array.
Difference between
for...of
andfor...in
Both
for...in
and for...of statements iterate over something. The main difference between them is in what they iterate over.The
for...in
statement iterates over the enumerable properties of an object, in an arbitrary order.The
for...of
statement iterates over values that the iterable object defines to be iterated over.
let arr = ["1,2,3", "iosajah", "undefined"];
for (let data of arr) {
console.log(data);
}
To get the elements in the order of the indices, you could take a standard for
statement and go from the first element to the end of the array.
let arr = ["1,2,3", "iosajah", "undefined"];
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
Upvotes: 1
Reputation: 21307
for in
loops over enumerable property names of an object, you are printing the indexes of the array. If you want to iterate over the values use one of the approaches:
for(let value of array)
Or
Object.keys(array).map(key => console.log(array[key]))
Upvotes: 3
Reputation: 1351
Your For/In statement is to traverse properties of an object. If you check the length of your array and step through it. We want values, rather than properties. You can print it this way.
let arr=["1,2,3","iosajah","undefined"];
for(i=0;i<arr.length;i++)
{
console.log(arr[i]);
if(typeof data === "undefined"){
//do something
}
}
Upvotes: 1
Reputation: 454
for in loop is used to iterate through object literals, but since Arrays (and everything else) are actually objects in JavaScript you are actually logging the keys of the Array object which are the index values.
You want to use forEach
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
let arr=["1,2,3","iosajah","undefined"];
arr.forEach(function(element) {
console.log(element);
});
Upvotes: 1