Why I don't see the loging properties only the length value?

Why I don't see the loging properties only the length value?
0

#1

Hello,

I try to push() an array properties to another array with for{} loop and when I console log() the outcome I only see the number of properties and not the acual value - The array - “Bob”, “Dylan”, 67

 var myArr = ["Bob","Dylan",67];
 var total = [];
        
 for(i=0; i<myArr.length; i++){
   total.push(i);
 }
 
console.log(total);

#2

Instead of pushing just i, which is the iterator, try pushing myArr[i], which is the value of myArr at index i.

total.push(myArr[i]);


#3

But you aren’t pushing the array elements, you are pushing your iteration variable, i.

total.push(i);

If you want to push your array elements, it would have to be:

total.push(myArr[i]);

#4

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Your code is adding the values 0, 1, 2 to total (which are indices). You start with i=0 and increment i at each iteration of the for loop, so your total.push(i) is just adding the current value of i during that iteration to total. If you want to add the actual value of myArr, then you must reference it and the index of myArr you want to get.

 for(i=0; i<myArr.length; i++){
   total.push(myArr[i]);
 }

See the following Free Code Camp challenge for more information about accessing array data.