This question already has answers here:
Short circuit Array.forEach like calling break
(30 answers)
Closed 5 years ago.
I have an object mobile_specs which have several fields such as DeviceName, Brand, Camera.
I loop though the mobile_specs object so that i can print the specifications of two mobiles in tabular format:
var i=0;
Object.keys(mobile_specs).forEach(function(key) {
if(i==5)
{
break;
}
var mobile1=mobile_specs.[key];
var mobile2=mobile_specs.[key];
alert(mobile1 + " " +mobile2);
i++;
});
But the above code give me an error which is:
Illegal break statement
How can i break my loop when i==5 ?
Any help is appreciated.
There is no way to stop or break a forEach() loop other than by throwing an exception. I think forEach is not suited for your job, use a simple loop instead
Related
This question already has answers here:
How to get value at a specific index of array In JavaScript?
(8 answers)
Closed 2 years ago.
so I am currently learning javascript on Codecademy. And a weird kind of thing got introduced that I don't quite get.
if you look at the code. you see at the end in console that after logging animals it is a [i] like why is that there? I get that is has something to do with the for loop. But I don't quite understand like why or what that it does. I don't know if question is clear enough but if you just try to explain why it is there and what it does there. that would be greatly appreciated:)
const animals = ['Grizzly Bear', 'Sloth', 'Sea Lion'];
for (let i = 0; i < animals.length; i++){
console.log(animals[i]);
}
the bracket notation is used to specify the index of the array you are looping through. You can always read more about javascript arrays on w3schools
This question already has answers here:
How to determine if an array has any elements or not?
(8 answers)
Closed 4 years ago.
Is possible to loop trough an array in php like we do it in JavaScript for example without using the for ( $X as $Y){}
For example we in JavaScript we can use this code :
var names=['john','tom','jane'];
for (i=0;i<names.length;i++){
names[i];
}
Now in the case of using the same method for this loop it would be this one and it gives us an error :
$names=['john','tom','jane'];
for ($i=0;$i<$names.length;$i++){
$names[$i];
}
So is there a way around this?
You can use count() for the length of the array.
$names = ['john', 'tom', 'jane'];
for ($i=0; $i < count($names); $i++){
echo $names[$i];
}
First of all the code gives you errors because you have written lenght wrong. It should be sizeof() or count().Secondly there is also another option for looping through array. That’s foreach. As I’m not on the computer you could check out the php manual for foreach and how to use it. I hope I was useful !
This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 4 years ago.
Don't know if I'm being blind but every time I look at this code the logic makes sense. I am trying to iterate through this for loop and for each iteration generate a dialog box with the ID of #dialog-(i) but it only show's the last iteration of 200. The code is below:
var i;
for(i=1;i<200;i++){
$("#dialog-" + i).hide();
$('#meetings_box-' + i).click(function() {
var dialog = $("#dialog-" + i).dialog();
if (dialog) {
console.log('yay');
console.log(dialog);
} else {
console.log('nay');
}
});
};
Any help to finding the issue, probably something really dumb
This is because click will happen sometime in the future and by then the loop has already finished it's execution and the value of i is updated to the last value. Instead of var you can use let
This question already has answers here:
Meteor - #each iteration of an array with another HTML element inserted after each nth item
(2 answers)
Closed 6 years ago.
I'm quite new to meteor blaze. I want to know how it's possible to display something (an advert) after a particular number of iteration in meteorjs. Any ideas? Thanks in advance.
And what about doing this within the js helper? I did it in this way.
Template.foo.helpers({
stuffWithAds : function(){
col = GivenCollection.find().fetch();
toTemplate = [];
for(i=0; i<col.length; i++){
toTemplate.push(col[i]);
if (i % 2 != 0){
toTemplate.push('whatever you want to insert');
}
}
return toTemplate; }
});
This inserts whatever you want after 2 elements.
This question already has answers here:
Determine whether an array contains a value [duplicate]
(18 answers)
Closed 9 years ago.
Is there a built-in function in javascript to do this or this is only the option to go? Please look at the code below:
var arr=[1,3,4,'+','-', or whatever]
function value_check(user_click){
var operators=['+','-','/','*','.']
for (var i=0;i<operators.length;i++){
if (arr[arr.length-1]==operators[i]){var value1='operator found';}
if (user_click==operators[i]){
var value2= value1;alert("consecutive operators"); break;
}
}
}
I think this code achieves what I intend to do but is there a simple and shorter way of doing this. In words, I want to achieve something like this:
if (arr[arr.length-1] && user_click BOTH ARE IN operators array)
alert("consecutive operators)
Yes, there are some options:
JavaScript indexOf()
jQuery.inArray()
arrayName.indexOf() is what you are looking for.