Questions tagged [for-in-loop]

'for-in' is a type of 'Fast enumeration', which is a language feature that allows you to efficiently and safely enumerate over the contents of a collection using a concise syntax.

for-in-loop
Filter by
Sorted by
Tagged with
109 votes
5 answers
158k views

Python for-in loop preceded by a variable [duplicate]

I saw some code like: foo = [x for x in bar if x.occupants > 1] What does this mean, and how does it work?
Greg Flynn's user avatar
  • 1,704
16 votes
4 answers
12k views

JavaScript Loops: for...in vs for

I faced a strange behaviour in Javascript. I get "Object doesn't support this property or method" exception for the removeAttribute function in the following code: var buttons = controlDiv....
Feyyaz's user avatar
  • 3,176
108 votes
10 answers
70k views

"var" or no "var" in JavaScript's "for-in" loop?

What's the correct way to write a for-in loop in JavaScript? The browser doesn't issue a complaint about either of the two approaches I show here. First, there is this approach where the iteration ...
futlib's user avatar
  • 8,328
8 votes
4 answers
6k views

Typescript element implicitly has type any with for...in loops

I have a JSON object imported from a JSON file (with resolveJsonModule: true). The object looks like this: "myobject": { "prop1": "foo", "prop2": "bar" } and it's type therefore looks like this: ...
Emrio's user avatar
  • 111
3 votes
4 answers
344 views

Inconsistent scope rules of variables in for, for-in and for-of loops

So I noticed that I have to use let inside a for loop, and cannot use const. However, I found that I can use const inside the for-in and for-of constructs (code below). Intuitively I can rationalize ...
dualscyther's user avatar
134 votes
7 answers
43k views

Type casting in for-in loop

I have this for-in loop: for button in view.subviews { } Now I want button to be cast into a custom class so I can use its properties. I tried this: for button in view.subviews as AClass But it ...
Arbitur's user avatar
  • 38.9k
56 votes
4 answers
23k views

When to use forEach(_:) instead of for in?

As documented in both Array and Dictionary forEach(_:) Instance methods: Calls the given closure on each element in the sequence in the same order as a for-in loop. Nevertheless, adapted from ...
Ahmad F's user avatar
  • 31k
33 votes
1 answer
56k views

For...in statement Objective-C

I am studying Objective-C and I came across this "for...in" statement. I searched for it but i still don't get how it works. Could someone explain to me in a noob-friendly how this statement works?
Augusto Dias Noronha's user avatar
14 votes
4 answers
4k views

Why is JavaScript's For...In loop not recommended for arrays? [duplicate]

I read somewhere (sorry, I can't find the link) that the For...In loop is not recommended for arrays. It is said here: http://www.openjs.com/articles/for_loop.php that it is meant for associative ...
John Bautista's user avatar
12 votes
2 answers
27k views

for..in or for..of Object keys

So my IDE doesn't like when I use a for..in loop to iterate over an object keys. I get a warning: Possible iteration over unexpected (custom / inherited) members, probably missing hasOwnProperty ...
Spedwards's user avatar
  • 4,374
7 votes
4 answers
11k views

Change properties of every item in an array?

I need to set the value of every item in this array, counting up. So, for example, path[0].value = 1, path[1].value = 2 etc... EDIT: I'm looking for the most efficient way to do this. I think a for ...
fatblacklip's user avatar
7 votes
2 answers
4k views

For-in loop and type casting only for objects which match type

I have seen answers here which explain how to tell the compiler that an array is of a certain type in a loop. However, does Swift give a way so that the loop only loops over items of the specified ...
Shuri2060's user avatar
  • 757
6 votes
4 answers
7k views

What is the difference between for..in and for each..in in javascript?

What is the difference between for..in and for each..in statements in javascript? Are there subtle difference that I don't know of or is it the same and every browser has a different name for it?
codymanix's user avatar
  • 29k
2 votes
1 answer
158 views

The order of looping through an object may be broken only during iteration?

I guess the preferred way of looping through an object is this: for (var prop in obj) { if( obj.hasOwnProperty( prop ) ) { console.log("obj." + prop + " = " + obj[prop]); } } MDN says that ...
Marina Dunst's user avatar
2 votes
5 answers
3k views

Adding functions to javascript's Array class breaks for loops

I was looking for a way to add max/min functions to JavaScript's Array class, which seemed to be a solved problem: JavaScript: min & max Array values?. However, when I tried using that, I started ...
Jeff's user avatar
  • 23
1 vote
1 answer
1k views

Assign object[key] to a temp variable in a javascript "for...in" loop?

I'm working on a JS to show different messages depending on the current url's hash. Now, that's all working, but I was wondering what the best way would be to access my messages (They'll be coded into ...
Cerbrus's user avatar
  • 71.8k
38 votes
1 answer
68k views

How to skip to next in javascript in a for-in with a while inside?

I have a short javascript code where I need to skip to next in the for loop....see below: var y = new Array ('1', '2', '3', '4'); for (var x in y) { callFunctionOne(y[x]); while (condition){ ...
Ram Iyer's user avatar
  • 1,424
13 votes
3 answers
25k views

With fast enumeration and an NSDictionary, iterating in the order of the keys is not guaranteed – how can I make it so it IS in order?

I'm communicating with an API that sends back an NSDictionary as a response with data my app needs (the data is basically a feed). This data is sorted by newest to oldest, with the newest items at the ...
Doug Smith's user avatar
  • 29.9k
8 votes
1 answer
1k views

Difference between a basic for-loop and a for-in-loop in JavaScript [duplicate]

Possible Duplicate: JavaScript “For …in” with Arrays In which situations using for (var i = 0; i < array.length; i++) is different from using for (var i in array) in ...
user avatar
7 votes
2 answers
782 views

Iterating with for .. in on a changing collection

I'm experimenting with iteration on an array using a for .. in .. loop. My question is related to the case where the collection is changed within the loop body. It seems that the iteration is safe, ...
Christophe's user avatar
  • 71.3k
5 votes
1 answer
796 views

How for in loop works internally - Objective C - Foundation

I found this answer: https://stackoverflow.com/a/5163334/1364174 Which presents how for in loop is implemented. NSFastEnumerationState __enumState = {0}; id __objects[MAX_STACKBUFF_SIZE]; ...
Paweł Brewczynski's user avatar
4 votes
2 answers
4k views

Javascript array iteration using for..in with MooTools included

I am iterating over an array in MooTools but seeing additional items when iterating through the array using the shorthand for..in loop. It works fine when I use the regular for loop. Is this a problem ...
Anurag's user avatar
  • 141k
3 votes
1 answer
11k views

Swift: For-in loop requires '[DeepSpeechTokenMetadata]' to conform to 'Sequence'

I'm running into a weird error with a for in loop and an array. it says For-in loop requires '[DeepSpeechTokenMetadata]' to conform to 'Sequence' Which doesn't make any sense... it knows it's an ...
Ryan Tremblay's user avatar
3 votes
2 answers
452 views

Explanation for in loop javascript

I'm having trouble understanding the way this for in loop works. function createSimpleNode(name, options, text) { var node = document.createElement(name); for (var o in options) { ...
Ryner's user avatar
  • 43
2 votes
2 answers
101 views

Should I use var in the for in construct?

I make use of a for in loop in a piece of JavaScript logic. Should I use the var keyword or not? When I run a for-in loop as per W3School's example, without the var then, as a side effect, it gets ...
Joeppie's user avatar
  • 447
0 votes
1 answer
65 views

javascript for in loop returning undefinded

Looking for help with a for in loop I am using in which returns undefined at the the end. I have 26 buttons, one button for each letter in the alphabet. I need to scan through the buttons so that ...
monkeyman905's user avatar
0 votes
2 answers
57 views

Javascript For...In syntax issue?

The search function in the following code is not working and I believe it has something to do with the For...In loop but I am new to JS and unsure why: var friends = { bill: { firstName: "bill", ...
Flexo1515's user avatar
  • 1,017
-1 votes
3 answers
5k views

Problems with JavaScript "for in" loop

I have an array of objects which will be the basis for a certain menu in my website. It will be build using JavaScript: [ {"menuName":"Contact Info","sectionName":"contacts"}, {"menuName":"...
Joseph's user avatar
  • 119k
-1 votes
3 answers
2k views

Remove duplicates from array of Int in Swift (for-in-loop)

There are a lot of methods to remove duplicates from an array in swift, but I'm trying to use a for in loop to manage that. Can any one explain, why this code doesn't work? Fatal error: Index out ...
Lenny's user avatar
  • 33
64 votes
8 answers
80k views

ESLint doesn't allow for in

I have an object currentValues= {hey:1212, git:1212, nmo:12121} and I use for in like this: for (const key in currentValues) { if (Object.prototype.hasOwnProperty.call(currentValues, key)) { ...
RamAlx's user avatar
  • 7,226
33 votes
5 answers
32k views

JavaScript: JSLint error "The body of a for in should be wrapped in an if statement to filter unwanted properties from the prototype"

I'm using the JSLint tool to ensure my JavaScript is "strict". I'm receiving the following error but don't understand how to fix it: The body of a for in should be wrapped in an if statement to ...
HeatherK's user avatar
  • 2,303
12 votes
2 answers
4k views

How do I return a sequence in Swift?

I'm trying to write an extension for the Matrix example from the book, slightly tweaked to be generic. I'm trying to write a method called getRow that returns a sequence of values at the given row. ...
Dan Abramov's user avatar
10 votes
2 answers
5k views

What is the difference between iterable and enumerable in JS? I am going through For/of and For/In loop and these terms are coming up frequently

I am coming across terms Iterable and Enumerable while studying For/in and For/of loops. Objects are supposed be enumerable and we have to use For/in loop to loop over the properties of the object and ...
Mahwash's user avatar
  • 101
8 votes
1 answer
6k views

Looping through all of the items in the window object

Last night, I got really bored and I thought of a little idea for a little script. Basically I was thinking about how many built-in functions PHP has compared to JavaScript and then I realized that I ...
Shawn31313's user avatar
  • 5,994
7 votes
4 answers
6k views

How to skip iterations of a for-in loop (Swift 3)

Is it possible to skip iterations of a for-in loop in Swift 3? I want to do something like this: for index in 0..<100 { if someCondition(index) { index = index + 3 //Skip iterations ...
MyBikeIsAwesome's user avatar
5 votes
2 answers
210 views

Iteration order of for.in &ndash; not by insertion (any more?)

According to my research, the order of keys in a for..in loop should be undefined/unreliable – but, if left undisturbed, should be in insertion order – but it's not: I fetch this data object from the ...
T4NK3R's user avatar
  • 4,325
5 votes
2 answers
2k views

Mutability of the Iterator Element in a For-In loop with an Array in Swift

I have some code in Swift 3.0 like so for trying to update the property in a array of elements... for point in listOfPoints { var pointInFrame : Float = Float(point.position.x * ...
FTNomad's user avatar
  • 175
4 votes
3 answers
1k views

Iterate over String.prototype

I am aware that the for in loop can help iterate through properties of objects, prototypes and collections. The fact is, I need to iterate over String.prototype, and though console.log(String....
xavier.cambar's user avatar
4 votes
3 answers
1k views

for...in loop not looping through all properties?

When I load my page, a nodeList gets created, and it looks like this: [text, h4, text, span, br, input, br, span, br, input, br, span, br, input, br, span, br, input, br] I created a simple for ...
bool3max's user avatar
  • 2,807
4 votes
1 answer
1k views

Is there a jQuery way of iterating over an objects own properties only?

I'm making a small jQuery-like library, and one thing striking me odd is the behavior of $.each. In javascript we have a for...in loop: for (var key in obj) { console.log(key + ': ' + obj[key]); ...
Camilo Martin's user avatar
4 votes
10 answers
3k views

difference between for loop and for-in loop in javascript

I found that there is a difference between for loop and for-in loop in javascript. When I define a new array: var a=new Array(); Then I put some value into in but not contiguously for example: a[...
Kent Lee's user avatar
  • 117
3 votes
3 answers
251 views

Javascript: Why use a for loop instead of a for-in loop for arrays?

I have been reading Object-Oriented Javascript by Stoyan Stefanov, and at one point he writes: The for-in loop is used to iterate over the element of an array (or an object, as we'll see later). ...
James's user avatar
  • 953
3 votes
5 answers
13k views

get value out of an object using a for in loop in javascript

I'm a JS newbie and am struggling to understand how to get the value out of an object when using a for in loop. Could you all share your knowledge with me please. Thanks! Here's my problem, the code ...
jstone's user avatar
  • 445
3 votes
3 answers
6k views

Function call inside loop taking only last iteration

my code look like this: if (ACTIVETICKETS.length > 0) { for (var m in ACTIVETICKETS) { if (ACTIVETICKETS.hasOwnProperty(m)) { var marker = new L.Marker(new ...
Akbar Ali's user avatar
  • 305
3 votes
3 answers
279 views

Javascript closures issues

So, I'm still reading Apress Pro Javascript Techniques and i'm having troubles with closures. As John Resig states: Closures allow you to reference variables that exist within the parent function. ...
user278064's user avatar
  • 10.1k
3 votes
1 answer
152 views

In Swift, can I use a for-in enumeration to initialize or reset an array?

I currently have an array in a Swift class that is of type Bool, declared as follows: public var cardIsTaken: [Bool] For purposes of keeping up with a Swift style guide that calls for avoiding ...
Charles's user avatar
  • 587
3 votes
4 answers
3k views

what is for....in statement in javascript

anyone can explain how to use for...in statement in javascript. I had read the w3school article but i think it is not so clear.Below is the code, please explain this: <html> <body> <...
dramasea's user avatar
  • 3,430
2 votes
2 answers
856 views

Add methods to Array.prototype, without the side effects

I'd like to add an "insert" method on Arrays. So I'm doing it like this: > Array.prototype.insert = function(index, element){ this.splice(index, 0, element); }; And it works: > a = [1,2,3]...
Tony Lâmpada's user avatar
2 votes
3 answers
3k views

JavaScript For-each/For-in loop changing element types [duplicate]

Possible Duplicate: JavaScript “For …in” with Arrays I'm trying to use the for-in syntax to loop through an array of numbers. Problem is, those numbers are getting converted to ...
Ishmael Smyrnow's user avatar
2 votes
2 answers
566 views

Parse PFFile download order iOS

I'm storing 5 PFFiles in an array and using getDataInBackgroundWithBlock to download those files from Parse. The problem is the order at which they appear in the table view cells is different every ...
twclark's user avatar
  • 43