for-in-loop

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

前提是你 提交于 2019-11-28 00:07:09
问题 This question already has an answer here: Why is using “for…in” with array iteration a bad idea? 27 answers 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 arrays, and in http://www.w3schools.com/js/js_loop_for_in.asp that is for iterating through all the properties of an object (It does not say that it can be used on arrays). I do not know who to

For…in statement Objective-C

旧街凉风 提交于 2019-11-27 13:33:39
问题 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? 回答1: See fast enumeration documentation. Basically you'd have, usually, an array, and you can obtain each item in the array with a handy loop instead of using NSEnumerator or an integer count variable. It makes your code much cleaner to ask for each NSString in your array rather than to have to

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

纵然是瞬间 提交于 2019-11-27 02:04:53
问题 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 Sequence Overview: A sequence is a list of values that you can step through one at a time. The most common way to iterate over the elements of a sequence is to use a for-in loop . Implying that iterating sequence by forEach(_:) or for in : let closedRange = 1...3 for element in closedRange { print

Type casting in for-in loop

删除回忆录丶 提交于 2019-11-27 00:18:56
问题 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 doesnt work and gives me an error: 'AClass' does not conform to protocol 'SequenceType' And I tried this: for button:AClass in view.subviews But neither does that work. 回答1: For Swift 2 and later: Swift 2 adds case patterns to for loops, which makes it even easier and safer to type cast in a for loop:

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

我只是一个虾纸丫 提交于 2019-11-26 23:38:14
问题 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 that this is because the for loop is implemented differently/is more primitive, whereas the other constructs desugar into for loops where the iterating variable is assigned at the top of the for loop. // Doesn't work for (const i = 0; i < 3; i++) { console.log(i); } // Works for (let i = 0; i < 3

“var” or no “var” in JavaScript&#39;s “for-in” loop?

断了今生、忘了曾经 提交于 2019-11-26 15:14:14
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 variable x is explicitly declared: for (var x in set) { ... } And alternatively this approach which reads more naturally but doesn't seem correct to me: for (x in set) { ... } Use var , it reduces the scope of the variable otherwise the variable looks up to the nearest closure searching for a var statement. If it cannot find a var then it is global (if you are in a strict mode, using strict , global

JavaScript Loops: for…in vs for

泪湿孤枕 提交于 2019-11-26 02:20:37
问题 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.getElementsByTagName(\"button\"); for ( var button in buttons ) button.removeAttribute(\'disabled\'); When I change the code with the following, the problem disappears: var buttons = controlDiv.getElementsByTagName(\"button\"); for ( var i = 0; i < buttons.length; i++ ) buttons[i].removeAttribute(\

Python for-in loop preceded by a variable

若如初见. 提交于 2019-11-25 23:29:06
问题 foo = [x for x in bar if x.occupants > 1] After googling and searching on here, couldn\'t figure out what this does. Maybe I wasn\'t searching the right stuff but here it is. Any input in debunking this shorthand is greatly appreciated. 回答1: The current answers are good, but do not talk about how they are just syntactic sugar to some pattern that we are so used to. Let's start with an example, say we have 10 numbers, and we want a subset of those that are greater than, say, 5. >>> numbers =