for-in-loop

Iterate over String.prototype

混江龙づ霸主 提交于 2019-12-10 15:27:15
问题 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.prototype) displays the complete prototype, when I do for (var prop in String.prototype) { console.log(prop); } to display the name of the elements in the prototype, it displays nothing, as if it were empty. Do the JavaScript engines hide the basic prototypes methods, or am I doing something wrong? 回答1:

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

别等时光非礼了梦想. 提交于 2019-12-10 15:22:20
问题 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]); } The problem with this, is that it will iterate over inherited properties as well, that is, properties coming from the object constructor's prototype. One can know this using hasOwnProperty, for example. And jQuery could do that. But, when you pass an object to $.each , it behaves exactly like a for.

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

落花浮王杯 提交于 2019-12-08 02:04:43
问题 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 indexed for loops when possible, I have something like this: for takenFlag in cardIsTaken { takenFlag = true } .. which gives me the error message "cannot assign to 'let' value 'takenFlag'" Out of curiosity, I tried declaring it with "var", as in: for var takenFlag in cardIsTaken { takenFlag = true } ..

Iteration order of for.in – not by insertion (any more?)

蓝咒 提交于 2019-12-07 03:11:12
问题 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 database, ordered by name: var travel = { '2': { name: 'bus', price: 10 }, '3': { name: 'foot', price: 0 }, '1': { name: 'taxi', price: 100 } } for (way in travel) console.log( travel[way].name ) // => taxi, bus, foot The keys get ordered numerically (in all of Chrome, Firefox, and Edge). Why?

Function call inside loop taking only last iteration

随声附和 提交于 2019-12-06 10:43:35
my code look like this: if (ACTIVETICKETS.length > 0) { for (var m in ACTIVETICKETS) { if (ACTIVETICKETS.hasOwnProperty(m)) { var marker = new L.Marker(new L.LatLng(ACTIVETICKETS[m].location.x, ACTIVETICKETS[m].location.y)); createHtmlForPopUp(m, function(data) { console.log(m); marker.bindPopup( data ); // calling a function with callback tile_layer.addLayer(marker); }); } } // for loop ends here } While executing this, I am getting only the last iteration of m. Total length of ACTIVETICKETS array is 16. So I am getting only 15 entered 16 time Tyler Biscoe The code below should work, but as I

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

本小妞迷上赌 提交于 2019-12-06 07:34:12
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 indexed for loops when possible, I have something like this: for takenFlag in cardIsTaken { takenFlag = true } .. which gives me the error message "cannot assign to 'let' value 'takenFlag'" Out of curiosity, I tried declaring it with "var", as in: for var takenFlag in cardIsTaken { takenFlag = true } .. which just gives me a whole slew of different, unrelated error messages. I am 99% sure it means at

Change properties of every item in an array?

妖精的绣舞 提交于 2019-12-05 11:03:35
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 loop is the best way, but I want to learn other ways. Can it be done with the map() method or forEach()? What about a for... in statement? I'd like to do it with pure JS, but if you can teach me a better way with jQuery, I'd be interested to learn that too. Thanks in advance. function Cell(x,y){ this.xCoordinate = x; this.yCoordinate = y; this.value; } var path = [new Cell(0,0), new Cell(0,1), new

Looping through all of the items in the window object

℡╲_俬逩灬. 提交于 2019-12-05 07:28:24
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 really don't know how many functions JavaScript actually has. I thought of writing a script that would look through the window object including every object inside the object and so forth. I wrote the script and it worked (tried it on a smaller object). However, my problem is that JavaScript wont let me loop though the whole windows object. I have tried: for (var key in window) { console.log(key);

Iteration order of for.in – not by insertion (any more?)

故事扮演 提交于 2019-12-05 06:12:35
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 database, ordered by name: var travel = { '2': { name: 'bus', price: 10 }, '3': { name: 'foot', price: 0 }, '1': { name: 'taxi', price: 100 } } for (way in travel) console.log( travel[way].name ) // => taxi, bus, foot The keys get ordered numerically (in all of Chrome, Firefox, and Edge). Why? And (since I was wrong) how can I iterate through them ordered by .name ? According to my research, the

How do I return a sequence in Swift?

拜拜、爱过 提交于 2019-12-03 07:30:28
问题 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. In C#, I would have written this: IEnumerable<T> GetRow (int row) { return Enumerable .Range (0, this.columns) .Select ((column) => this.grid[row, columns]); } or alternatively IEnumerable<T> GetRow (int row) { for (var column = 0; column < this.columns; column++) { yield return this.grid[row,