for-in-loop

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

夙愿已清 提交于 2019-12-25 01:09:47
问题 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 below just logs the properties and I'm trying to use a for-in loop to find if an object contains the value "apple" var mac = { company: 'apple', product: 'iPhone', price: 300 }; for (var key in mac) { console.log(key); } 回答1: Try this: for (var key in mac) { if (mac[key] === 'apple'){ console.log(

Add methods to Array.prototype, without the side effects

好久不见. 提交于 2019-12-24 01:54:57
问题 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] [1, 2, 3] > a.insert(0, 4) undefined > a [4, 1, 2, 3] But there's an undesired side effect: > for (i in a){console.log(i)} 0 1 2 3 insert > for (i in a){console.log(a[i])} 4 1 2 3 function (index, element){ this.splice(index, 0, element); } This behavior is not intended and breaks other libraries that I use

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

岁酱吖の 提交于 2019-12-23 13:24:28
问题 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 * sensorIncomingViewPortSize.width) + Float(point.position.y) point.status = getUpdateStatus( pointInFrame ) } However I get a compile error of: 'Cannot assign to property: 'point' is a 'let' constant' [for line 3] Is there anyway to make the iterator (point) mutable in Swift, like how you can use 'inout' for a function

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

只愿长相守 提交于 2019-12-23 13:24:21
问题 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 * sensorIncomingViewPortSize.width) + Float(point.position.y) point.status = getUpdateStatus( pointInFrame ) } However I get a compile error of: 'Cannot assign to property: 'point' is a 'let' constant' [for line 3] Is there anyway to make the iterator (point) mutable in Swift, like how you can use 'inout' for a function

For .. in loop?

☆樱花仙子☆ 提交于 2019-12-23 04:28:05
问题 I'm losing it here.. I am now extremely confused about how this loop works. From w3 schools: var person={fname:"John",lname:"Doe",age:25}; for (x in person) { document.write(person[x] + " "); } person is an object with properties right? How are those properties being accessed with the brackets? I thought that was for arrays? Why does this also work, and shouldn't it ONLY be like this?: var person=[]; person["fname"] = "John"; person["lname"] = "Doe"; person["age"] = "25"; for (x in person) {

Function call inside loop taking only last iteration

五迷三道 提交于 2019-12-22 18:33:30
问题 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

Change properties of every item in an array?

ぃ、小莉子 提交于 2019-12-22 06:49:25
问题 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){

Looping through all of the items in the window object

二次信任 提交于 2019-12-22 05:13:19
问题 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

Javascript array iteration using for..in with MooTools included

自闭症网瘾萝莉.ら 提交于 2019-12-20 01:08:56
问题 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 with MooTools polluting the global namespace or am I doing something wrong here? There is a createTabs() function that iterates over an array and creates a tab for each value in the array: function createTabs() { var myTabs = [["First", "a.png"], ["Second", "b.png"]]; for(var i in myTabs) { var

for..in or for..of Object keys

ぃ、小莉子 提交于 2019-12-19 09:17:13
问题 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 check So I get what it's saying, so in that case would it be better to use something like for (const key of Object.keys(obj)) instead of for (const key in obj) ? Are there any real differences between the two, performance-wise? 回答1: There is a slight difference between looping though the Object.keys