How to create a JavaScript For Each Loop [duplicate] - javascript

This question already has answers here:
Loop (for each) over an array in JavaScript
(40 answers)
Closed 8 years ago.
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);
}

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

Related

Javascript push and loop [duplicate]

This question already has answers here:
Create an array with same element repeated multiple times
(25 answers)
Closed 2 years ago.
I currently have this array: var arr = []
How do I push multiple "hello" strings into the array using a for loop?
I tried
var newArray = arr.push("hello")10;
try new Array forEach or simple for-loop should work.
var arr = [];
// method 1
new Array(5).fill(0).forEach(() => arr.push("hello"));
// alternate method
for (let i = 0; i < 5; i++) {
arr.push("world");
}
console.log(arr);
// Updating based on suggesion #mplungjan, quick way without loop.
var arr2 = Array(10).fill("hello");
console.log(arr2)

Stupid questions, iterating over JSON [duplicate]

This question already has answers here:
Iterate through object properties
(31 answers)
Closed 6 years ago.
So a two part question, because I can't solve the first problem, and the hacky work around isn't seeming to work either.
I have a JSON object, and within that object is nested object. I want to manipulate something within a deep layer array.
So to start with, I need to learn how to iterate over keys in an object, but I can't see how you do it.
Say I have an object with objects within it, how do I iterate over these?
If it were an array I would do
for (i = 0; i < arrayLength; i++)
{console.log(array[i])}
But, because they're words, I can't just i++, so I made an array of the words, then wanted to do the same as above, but
for (i = 0; i < arrayLength; i++)
{console.log(jsonObject.array[i]}
, but this isnt seeming to work, I just get undefined returned.
Apologies for the poor explaination.
There is a method to get the keys and then you can iterate with those.
var keys = Object.keys(obj);
for(i = 0 ; i < keys.length; i++){
var result = obj[keys[i]];
console.log(result);
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
You need to use a 'for..in' loop, or, even better, Object.keys() handles this well.
Object.keys() documentation here.
for (var i in myObject) { console.log[i] };
You iterate using for (key in obj) {...} syntax.
As in:
var t = document.getElementById('myTextField');
var obj = {name:'John',last:'Doe'};
var result = '';
for (key in obj) {
result += key + '=' + obj[key] + ',';
}

How to remove all the elements from array [duplicate]

This question already has answers here:
How do I empty an array in JavaScript?
(17 answers)
Closed 6 years ago.
I have an array eg: a[3,4,5,6,7,8]. I want to remove all the element in one time and make array as an empty array. How to remove all the element of an array.
My code
var a = [2,3,4,5,6];
for(var i=0; I<a.length; i++){
a.remove();
}
a.length = 0;
This is all what you need
var a = [2,3,4,5,6];
console.log(a);
a.length = 0;
console.log(a);
Do a.length = 0; if you don't want to lose references. Do a = []; if you want to lose references.

how to create array object using integer variable javascript [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 7 years ago.
I need to fill an array object as like below using integer variable.
for(i=1; i<=2; i++)
arr[i].push({i:(100 * i)})
Expected result is:
arr = [{ 1:100,2:200},{1:100,2:200}]
Problem is, array created as like below
arr = [{i:100,i:200},{i:100,i:200}]
You need to push to arr instead of arr[i].
Also, you can't use a variable as a key in json directly.
var arr = [];
for(i=1; i<=2; i++)
{
var b = {};
b[i] = 100*i;
arr.push({[i]:(i*100)});
}
console.log(arr);

For ... in for an array of strings [duplicate]

This question already has answers here:
Loop (for each) over an array in JavaScript
(40 answers)
Closed 7 years ago.
For me this looks odd:
products = ["Item1", "Item2", "Item3"];
for (var x in products) {
debugger;
console.log(x);
// x === "0" instead of "Item1"
}
I wonder why?
for..in loops over enumerable properties and arrays have numerical properties which acts as index. It is to be used with only objects.
Doing so with Arrays will also give you properties which you won't be interested in(such as those properties which are on the higher chain of prototypic inheritance from Object object)
So use a simple for loop or Array.forEach
products.forEach(function(str){
console.log(str);
});
// or
for(var i = 0; i < products.length; i++)
console.log(products[i]);
That's because in your case, variable x is holding the array item's index, and not the value. Instead of x, you should use products[x].
products = ["Item1", "Item2", "Item3"];
for (var x in products) {
debugger;
console.log(products[x]);
}
Now, instead of:
0
1
2
you'll get
Item1
Item2
Item3
Iterate over the array like this.
U use var in arr if you want to iterate over properties of an object, not an array!
var products = ["Item1", "Item2", "Item3"];
for (var i =0; i< products.length;i++) {
debugger;
console.log(products[i]);
// x === "0" instead of "Item1"
}

Categories

Resources