Menu
  • HOME
  • TAGS

Sum up several array through several objects

javascript,arrays,object,for-loop,for-in-loop

There's a few problems with your code. You are clearing the total array each time you step in to the inner loop. To fix this, declare the array outside of both loops. You need to initialize total with 7 indices (all starting at zero). You need to increment total at...

difference between for loop and for-in loop in javascript

javascript,arrays,for-loop,for-in-loop

for (... in ...) is typically used to iterate through the properties of objects (which are what javaScript uses for associative arrays), while the typical for loop is used for sequential arrays. In your example, you are really creating an associative array with the keys 0, 1, and 4. If...

How to generate an array of buttons using a for-in-loop and then animate them with a CADisplayLink

ios,swift,uibutton,for-in-loop,cadisplaylink

The constructor Array(count:, repeatedValue:) does not execute the UIButton constructor multiple times. It receives a value and then repeats it, you just happen to instantiate the pointer in-line. What you have done is functionally the same as: var aButton:UIButton = UIButton.buttonWithType(.System) as UIButton var buttons: [UIButton] = Array(count: 10, repeatedValue:aButton)...

Why does this JS for-in loop log just the key of a property, and not the entire property?

javascript,arrays,for-in-loop

That is the intended behavior of the javascript for in loop. If you want the value of the property then you should do this: var myObject = {0:'cat', 1:'dog', 2:'fish'}; for (var x in myObject) { console.log(myObject[x]); } In your example object you have three properties. The names of those...

Explaination needed: Different outputs when used for…in and for(;;) in JavaScript

javascript,node.js,for-loop,for-in-loop

What you're doing is wrong. You're removing keys from the array whilst looping through the same array. Your for...in loop will only ever perform 7 iterations, as 4 of your keys are spliced from the array whilst the array is still being iterated through, whereas your for(;;) loop will always...

Javascript for-in loop issue

javascript,arrays,for-in-loop

In a for-in loop, the variable is set to the property names. So if you want to log the property name, use console.log(p). friends[p] is the value that the property name maps to in the object.

Cannot change textColor and font size inside the loop in swift

ios,swift,uibutton,for-in-loop

First remove the line Button.titleLabel?.text = "\(button)". This one is enough Button.setTitle("\(button)", forState: UIControlState.Normal). Then you should use setTitleColor method from a button to change it. Button.setTitleColor(UIColor. blackColor(), forState: UIControlState.Normal) Finally you made a mistake in the name of your font : Button.titleLabel?.font = UIFont(name:"ChalkboardSE-Regular", size: 30.0) That's it!...

Getting the name of an object in a for in loop to use as a key for another object

javascript,object,key,for-in-loop

maybe this is what you are looking for: var Focus = function(config){ this.points = config || {}; this._focus = undefined; }; Focus.prototype.add = function(point){ this.points[point.name] = point }; /* or if you dont want the property name Focus.prototype.add = function(id, point){ this.points[id] = point }; */ Focus.prototype.setFocus = function(focus){ this._focus...

accessing an array within an object javascript

javascript,arrays,for-loop,dynamic-arrays,for-in-loop

Iterating an object with for..in is okay, but not an array. Because when you sue for..in with an array, it will not get the array values, but the array indices. So, you should be doing something like this for (var key in myObject) { var currentArray = myObject[key]; for(var i...

Counter as variable in for-in-loops

variables,swift,constants,declaration,for-in-loop

To understand why i can’t be mutable involves knowing what for…in is shorthand for. for i in 0..<10 is expanded by the compiler to the following: var g = (0..<10).generate() { while let i = g.next() { // use i } Every time around the loop, i is a freshly...

for in loop behaving strangely in javascript

javascript,for-in-loop

friend here is the index of your array, not the value at the index var room = 'userid'+friends[friend]+'friends'; Plus when looping through an array I don't recommend to use for..in loops, you can use Array.prototype.map or plain for loop with the length of your array Array map example: var friends=['2','3'];...

Parse PFFile download order iOS

ios,objective-c,iphone,parse.com,for-in-loop

The question has two parts: (1) explicitly, how to maintain the order of results of asynchronous operations, (2) implied by the use of cell, how to properly handle asynch requests in support of a tableview. The answer to the first question is simpler: keep the result of the request associated...

How to implement a CADisplayLink into a for in loop

ios,swift,for-in-loop,cadisplaylink,animatewithduration

You'll only want to add one CADisplayLink. var displayLink: CADisplayLink! override func viewDidLoad() { // ... for loopNumber in 0...100 { // ... } displayLink = CADisplayLink(target: self, selector: Selector("moveDots")) displayLink.frameInterval = frameInterval displayLink.addToRunLoop(NSRunLoop.mainRunLoop(), forMode: NSRunLoopCommonModes) } func moveDots() { for dot in self.subviews { // calculate its new position...

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

arrays,swift,for-in-loop

The best way to do this is to use the array's built in mapping function. cardIsTaken = cardIsTaken.map { isTaken in true } ...

Find the index of an Array string value using a randomly generated string

ios,arrays,swift,nsuserdefaults,for-in-loop

I agree with above comment that it would be easer to store the index instead of the string in the user defaults. If that is not possible: NSArray has a indexOfObject() method which returns the first index of an object in an array: let index = factsArray.indexOfObject(i) if index !=...

Looping through an array of dictionaries in Swift

arrays,dictionary,swift,ios8,for-in-loop

The problem is with the venuesArray the loop is expecting the array type not single object: In your code : let venuesArray: AnyObject = // this is not array, let venuesArray: AnyObject[] = // this could be the array of anyObject's Try accordingly to accomplish your goal. ...

Add segments from a UISegmentedControl to an NSArray

ios,objective-c,nsarray,uisegmentedcontrol,for-in-loop

There are only two possible reasons for what you see: None of the array's elements is a UILabel There are no elements in the array. This could be because [self.segment.subviews objectAtIndex:0] returns nil, which only means that the self.segment is nil. Try to explore the view hierarchy, before you assume...

Explanation for in loop javascript

javascript,loops,for-in-loop

The For in loop gives a way to iterate over an object or array with each value and key. It can be applied over an object or Array. For an Object For an object it gives each key in the object as the ITER variable. Using this variable you can...

Control statement confusion in Python [duplicate]

python,for-loop,for-in-loop

The \ is a line continuation, allowing the statement to continue to the next line without raising an indentation error. Aside from that this is just a vanilla for loop.

Using typeof within a for-in loop in javascript

javascript,typeof,for-in-loop

It should be if(typeof family[prop] === "string") for(prop in family){ if(typeof family[prop] === "string"){ console.log(family[prop]); } } prop represents the key and its always "string" whereas you need to use family[prop] which will return you the value you have stored in the object...

How do I return a sequence in Swift?

generics,map,generator,swift,for-in-loop

Joe Groff suggested to wrap the result in SequenceOf<T>: extension Matrix { func getRow(index: Int) -> SequenceOf<T> { return SequenceOf(map(0..self.columns, { self[index, $0] })) } } Indeed, this works but we had to wrap map result into a helper class which differs from how I do it in C#. I...

How to iterate over an NSArrayController contents in swift?

swift,nsarraycontroller,for-in-loop,xcode7

You can do something like this: for element in downloadingFilesArrayController.arrangedObjects as! [AnyObject] { // want to do some useful things on element } ...

Loop through severals objects js

javascript,jquery,loops,object,for-in-loop

That's because your selector $('.detail') selects all elements with class detail. Try using the DOM instead of strings. I give you this as a personal advice. I used to append HTML via strings just like you're doing it now. Since I started using DOM objects as they are actually meant...

Clearing every element of Arrays nested inside a Dictionary

arrays,dictionary,swift,for-in-loop

You should be accessing category.keys, like this: func eraseContent() { for index in category.keys { category[index] = [] } } ...

Can't assign values to properties of object with a “for…in” loop

javascript,javascript-objects,for-in-loop

You want to do: cornersAbs[corner]=... to access the 'corner' you are looking at. To see a sub-property, you use cornerAbs[corner].prop, which indexes the property referred to by corners and looks at prop within it. On the other hand, cornerAbs.corner is equivalent to cornerAbs["corner"], which is not what you want....

for…in looping working in Chrome, but not in IE?

javascript,google-chrome,internet-explorer,for-loop,for-in-loop

I can't find the reference but I am fairly sure that some versions of IE do not support indexing strings, you are supposed to use the the charAt function. That being true, in those versions of IE, the for .. in has nothing to iterate over for a string. By...

Specify for in loop type [duplicate]

xcode,swift,for-loop,for-in-loop

Yes, this is a common problem. The solution is to cast: for item in items as [NSString] { It is perhaps a little surprising that you have to cast the array (items) rather than explicitly declaring the type of the loop variable (item). But that's the syntax, and you'll quickly...

Looping through an object and a nested for loop that loops through array

javascript,arrays,object,for-loop,for-in-loop

Your object has duplicate keys. This is not a valid JSON object. Make them unique Do not access the object value like obj[prop].a, obj[prop] is a Clone the original array. Use indexOf() to check if the array contains the object property or not. If it does, remove it from...

Do Python 2.7 views, for/in, and modification work well together?

python,python-2.7,dictionary,concurrentmodification,for-in-loop

Yes, this applies to dictionary views over either keys or items, as they provide a live view of the dictionary contents. You cannot add or remove keys to the dictionary while iterating over a dictionary view, because, as you say, this alters the dictionary order. Demo to show that this...

Swift for-in loop dictionary experiment

dictionary,swift,control-flow,for-in-loop

They're just asking you to keep track of which number category the largest number belongs to: let interestingNumbers = [ "Prime": [2, 3, 5, 7, 11, 13], "Fibonacci": [1, 1, 2, 3, 5, 8], "Square": [1, 4, 9, 16, 25], ] var largest = 0 var largestkind = "" for...

Looping through an object and putting result to an array with javascript

javascript,arrays,object,for-loop,for-in-loop

If you just want the ones in vehicles.list: var arr = Object.keys(vehicles.list); var vehicles = { list:{ "transport":{ //<-- This is the values I want to put to an array name:"transport", pixelWidth:31, pixelHeight:30, pixelOffsetX:15, pixelOffsetY:15, radius:15, speed:15, sight:3, cost:400, hitPoints:100, turnSpeed:2, spriteImages:[ {name:"stand",count:1,directions:8} ], }, "harvester":{ //<-- This is the...

Can I use 'where' inside a for-loop in swift?

for-loop,swift,where,where-clause,for-in-loop

In Swift 2, new where syntax was added: for value in boolArray where value == true { ... } In Pre 2.0 one solution would be to call .filter on the array before you iterate it: for value in boolArray.filter({ $0 == true }) { doSomething(); } ...

looping through a string with foreach

c#,string,types,foreach,for-in-loop

try this and it will be fine foreach (char num in output.ToCharArray()) StringBuilder.AppendFormat("<p value=\"{0}\"></p>",num.ToString()); ...

How to dynamically populate an array using a for in loop

javascript,arrays,for-in-loop

The second link seems to have your answer. The for (var item in array) looks at your array and, for each item in the array, stores that item in "item" and lets you iterate over each item. Setting the array.length does not give you any items in the array, and...

For/In Loop with a new object

javascript,key,for-in-loop

Use the following javascript var me = new Object(); me.name = "Brody"; me.age = "18"; alert(me.name); Updated version var me = { "name" : "Brody", "age" : "18" }; for(var key in me) { if(key == "name") alert(me[key]); } Working Demo...

Trouble using width for String in for-in cycle

swift,for-loop,for-in-loop

in case you run into this kind of problem, always do a println() of the variable you are using println("\(age)") right before let paddedAge = "\(age!)".leadingSpaces(3) reveals the problem age is an optional, meaning that you are trying to do the padding on a String which has this value "Optional(17)"...