Okay, take it easy on me. I am really new to JavaScript and having issues getting the for-each loop to work correctly. Any Tips?
var array = ["Bob", "Nancy", "Jessie", "Frank"]; var arrayLength = myStringArray.length; for (var i = 0; i < arrayLength; i++) { document.write(array); }
foreach will copy the array structure if and only if the iterated array is not referenced and has a refcount > 1.
The JavaScript forEach method is one of the several ways to loop through arrays. Each method has different features, and it is up to you, depending on what you're doing, to decide which one to use.
The Set object lets you store unique values of any type, whether primitive values or object references. you are passing new object not reference so it is allowing to add duplicate.
var myArray = ["Bob", "Nancy", "Jessie", "Frank"]; var arrayLength = myStringArray.length; for (var i = 0; i < arrayLength; i++) { //Do something with element myArray[i] }
I guess you need something like this.
Edit: Your array has only 4 elements. In the 2nd line I save the length of your array (4 elements --> length is 4) in the variable 'arrayLength'. Then I wrote a simple for-loop which cycles the 'i' from 0 till 3 so you can access your elements from your array as 'myArray[i]'.
The for in
is used to iterate over properties on the object. It is not the same as a regular foreach. Use a for loop for this
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With