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
0 votes
4 answers
42 views

"for in" loop --- object syntax

when I write for (var x in object) { console.log(object[x]) } it works fine! But if I replace object[x] with object.x it doesn't work, it returns undefined. console-logging both object[...
Youness El Azzaoui's user avatar
2 votes
1 answer
49 views

Super simple nested Pythonic for-in loops not functioning correctly... something to do with inputs

I have a simple piece of code that is not working and I can't begin to figure out why. The code is as follows: def myFunction(otherDictionary, mongoDbCollection): for p in otherDictionary: ...
learningToCode54321's user avatar
0 votes
1 answer
41 views

for-in loop to for loop or forEach

Ok I have the following scenario. I need to convert this for-in loop to either a for loop or forEach. I have tried a few different examples but can't seem to get the code to append to the page. The ...
Chad Andersen's user avatar
0 votes
1 answer
93 views

breaking down for in loops for units and peers

Following understanding of list comprehensions now I’m trying to decipher units and peers from following code, if someone can please breakdown the nested structures. The intended output is clear to me ...
Developer's user avatar
  • 954
1 vote
2 answers
696 views

Swift For In Loop Iterating Through an Array of Custom Classes Doesn't Work

I am working a project and have come across this error when iterating through a for in loop like this: class CustomClass { var nameNum : Int { didSet { self.name = "CustomClass \(nameNum)...
grahamcracker1234's user avatar
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
0 votes
2 answers
132 views

Python for in loop to generate dictionary

I came across this snippet of code, I understand it's a Cartesian product but if someone can break up this line for me [s+t for s in a for t in b] from the following code along with docs link for this ...
Developer's user avatar
  • 954
0 votes
1 answer
67 views

Strange behaviour "for in" statement (NodeJS) [duplicate]

I think I miss something into the use of "for in" statement. I have a JSON document returned from a mongoDB query (nodejs + mongoose), his structure is like the following: [{ "_id":"...
Nicola's user avatar
  • 475
2 votes
1 answer
663 views

why for in loop showing undefined value [duplicate]

I am having trouble understanding how this for-in loop showing Undefined values at the end in JavaScript, but in loop the length of first is 3. So, why so many undefined values. <div class="tab-...
user avatar
0 votes
2 answers
3k views

Creating a for/in loop in an Angular 2 template

Is there a way to iterate over the properties of a static (Typescript) class, inside of an Angular 2 template? In other words, I want a for/in loop which iterates over an object instead of an array (...
JazzyGreen's user avatar
2 votes
3 answers
43 views

iterations thru objects JavaScript

Hello everybody I have a short question about iteration in objects Here's my code [https://jsfiddle.net/Ar2zee/9g91ouq6/] var names = { first: 'Sasha', second: 'Andrey', third: 'Pasha',...
Ar2zee's user avatar
  • 410
0 votes
2 answers
49 views

Accessing a nested object with loop but too many undefines?

I'm working with an object literal and trying to access the nested array and iterate through a loop but it keeps displaying 59 times and 5 times again as undefined in my console but then displays the ...
logan26's user avatar
  • 283
0 votes
0 answers
94 views

Eloquent Javascript Squirrel + Pizza Correlation code

Im struggling to understand what the computer is "thinking" when you tell it this "for in" loop: var map = {pizza: 0.069, touched tree: -0.081}; for (var event in map) console.log(map[event]);
Bijan Khadembashi's user avatar
-1 votes
2 answers
92 views

How do I use For...In f{a,b,c,d}c, but with variables?

I'm trying to make a loop in Bash. I tested the loop with for adr in a{a,b,c}d and it works perfectly with aad, abd, acd But if I have a variable with the value a{a,b,c}d, it doesn't return the above ...
Yash Sharma's user avatar
-1 votes
2 answers
233 views

One line conditional assignment if statement

This is the first question I am asking on this forum, so I welcome your feedback on making this more helpful to others. Say I have this list: IDs = ['First', 'Second', 'Third'] and this dictionary: ...
Moe's user avatar
  • 492
0 votes
1 answer
61 views

Is there a way to access an object literal's name?

I have an object: var Top = { 'A':{}, 'b':{ '1':'someText', '2':'someMoreText'}, 'C':{ '3':'evenMoreText', '4':'thisIsGettingRedundant'}, 'D':'...
Spencer Cornwall's user avatar
2 votes
1 answer
143 views

Possible to access 'root' model in underscore template?

In underscore normally when an object is passed in as the model, the properties will be directly accessible within the template. MODEL var obj { prop1: 'a', prop2: 'b' } TEMPLATE <p>...
Paulos3000's user avatar
  • 3,485
1 vote
7 answers
9k views

Swift 3.1 pattern with loops

I have a C program like this that I want to translate to Swift: int main(void) { int i, j; for(i=1;i<=5;i++) { printf("\n"); for(j=1;j<=5-i;j++) { ...
Tushar Katyal's user avatar
1 vote
5 answers
10k views

Swift fatal error: Can't form Range with upperBound < lowerBound

I have a class that I want to add value there with for loop. This is my class: public class Expandable { public var name: String public var id:Int init(name: String, id:Int) { ...
jorjj's user avatar
  • 1,529
0 votes
1 answer
84 views

Numbers of rows to process

I would like to make sure that an e-mail is not send twice. The script itself is working, however the Insertion of: sheet.getRange(startRow + i, 6).setValue(EMAIL_SENT); does not work, yet it adds a ...
user8014703's user avatar
4 votes
2 answers
14k views

PL/SQL - Delete current record in FOR IN LOOP

I am trying to figure out how to delete a record using the FOR .. IN .. LOOP FOR REC IN (SELECT * FROM WORKLIST) LOOP GET_DATA(REC); --DELETE REC FROM WORKLIST END LOOP; The GET_DATA procedure ...
Delphy's user avatar
  • 306
0 votes
0 answers
55 views

Why for...in and for...of behave differently in the same case? [duplicate]

I read the explanations of for...in and for...of but then faced this situation where in the same case they behave differently. //case1 function avg(...args) { var sum = 0; for (let value ...
Aaron's user avatar
  • 379
0 votes
2 answers
71 views

Loop through object and get other objects (javascript)

I have an object with other objects inside of it that I need to access. How do I do it? var obj = {a: {b: 1}} //I need to have access to b. Right the for in loop just returns "a". The problem is I'm ...
Invincible Blaze's user avatar
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,228
-2 votes
3 answers
233 views

How does "for in" loop work in Javascript?

The program is as follows, function sum(x) { console.log(typeof x[0]); //return Number var s=0; for(var e in x) { console.log(typeof e); //This return string s+=e; } return s; } ...
Tarun Maganti's user avatar
1 vote
2 answers
6k views

Swift: For Loop to iterate through enumerated array by index greater than 1

Is there a way to use a for-in loop through an array of strings by an index greater than 1 using .enumerated() and stride, in order to keep the index and the value? For example, if I had the array ...
Biggytiny's user avatar
  • 529
0 votes
1 answer
30 views

Having trouble fulling understanding object iteration with a For-In loop

guys. First off, thanks to all who are taking their time to help a guy out. I'm currently going through object iteration with a for-in and right now, I'm having a bit of trouble trying to print out ...
Kenneth McAusland'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
1 vote
2 answers
259 views

Javascript For..In loop executing both the if and else statement

I have this if statement with an else block and they are both inside a for in loop. When I execute this it always return both the value from the if statement and the else statement. Shouldn't it only ...
Amos's user avatar
  • 11
1 vote
3 answers
4k views

Typescript: For in loop over objects won't recognise object's properties

I'm using a for in to loop over a list of Meeting-objects called allMeetings. This loop is filling another list called allEvents, where objects other than Meeting will end up in. In that loop I'm ...
Wouter Vanherck's user avatar
0 votes
2 answers
1k views

Javascript, looping through an array and the arguments "object"

I can't get my head around this, i'm using filter to loop through an array and filter out all the integers passed as arguments ,i'm not limited in the number of arguments. But i'm stuck here when it'...
Matthieu Ducorps's user avatar
0 votes
3 answers
50 views

Javascript for/in loop - join items from two different objects?

I have these datasets below and I want to joint the data in each set: var data = {}; var datasets = [ {"_id":"58d6c806d7c80d5d44a35204","particles":{"timestamp":[1490470918708,...
Run's user avatar
  • 56.1k
0 votes
2 answers
424 views

Make each button(color) from list turn background to correspondent color

I want to make each button (under the colors list) change the (overall window) background color to the one from its name when clicked. I guess that's kinda what I aimed at with this part of the code ...
Marco Lozano's user avatar
-3 votes
1 answer
142 views

Javascript class method vs prototype property

for-in example image http://jsbin.com/mosedibera/edit?js,console function Point(x, y) { this.x = x this.y = y } Point.prototype.point = function() { console.log('point') } var point = new ...
MingSian Tu's user avatar
0 votes
2 answers
2k views

Character in String array swift

I'm trying to find the position of the letters that make up a string. For example, I need to know the positions of the letter "c" in the word "character" for further calculation. I tried for ...
Yiu Chung Wong's user avatar
-1 votes
2 answers
217 views

For/In loop in JavaScript

My values of delivInfo elements are undefined. The ids match the html file but I am getting a blank output. Kinda lost. Edit: https://plnkr.co/edit/tTQVvr8MxBsAkzhHxRlz?p=catalogue for html code ...
Koochi's user avatar
  • 99
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
4 votes
2 answers
10k views

What is the best way to append an array using for in loop in Swift?

I am currently attempting to learn how to code swift via treehouse.com and so far I am enjoying it. I just came to my first "code challenge" as they call it that I somewhat struggled on. As the title ...
Will DeBernardi's user avatar
0 votes
2 answers
5k views

Variable undefined inside JavaScript For-In loop [closed]

I stumbled upon this puzzling error when looping over JavaScript objects using the for..in construct. I have a set of dummy data similar to this: { "12345678" : { ... }, "12345679" : { ....
ppajer's user avatar
  • 3,105
0 votes
1 answer
81 views

JS Object property copying: what is happening behind the scenes?

Let's say I want to copy two objects the old way: var firstObj = { key1: "val1", key2: "val2" } var secondObj = { key1: "val1", key2: "val2", key3: "val3", key4: "val4" } If I use the ...
vzR's user avatar
  • 1,407
-1 votes
3 answers
818 views

Populate <p> tags with JSON data for in loop

I'm trying to target 18 $('.char-names') elements and dynamically populate with names from a local JSON file. The 3 console log's are showing 18 different id, charName, ingName, so the data is linking....
Orbital676's user avatar
29 votes
7 answers
84k views

Type Int does not conform to protocol sequence

I have the following code in Swift 3: var numbers = [1,2,1] for number in numbers.count - 1 { // error if numbers[number] < numbers[number + 1] { print(number) } } I am checking ...
Toby V.'s user avatar
  • 455
0 votes
4 answers
92 views

string.includes only checking the last value of a for in loop javascript

I'm making a webpage that displays a motivational quote based on a "mood keyword". I have an object with arrays: const moodList = { sad: [ '"Dream as if you\'ll live forever, live as if you\'ll ...
Amos's user avatar
  • 11
0 votes
3 answers
56 views

JavaScript Object Structure Issue

I am having an issue traversing the objects returned from uploaded file data, but it appears that potentially the object structure that is being returned is preventing me from capturing specific ...
cphill's user avatar
  • 5,776
16 votes
3 answers
12k views

Swift - Unwrap optional in for in loop with where clause

I have a class with an optional member : class A { var i: Int? = nil } Then I have an array of objects of type A. Some objects in the array have a value for i, some others don't. I want to ...
Vince's user avatar
  • 545
2 votes
4 answers
3k views

Iterating through window object using for in loop

Basically i want to print all properties on the window object, i am using: for(var k in window){ document.write("window object = "+ Object.getOwnPropertyNames(window[k])+ "<br>"); } I get a ...
MrShabana's user avatar
  • 377
1 vote
4 answers
157 views

Changing for in loop to for loop

Imagine I have an object called projects. The projects object has a key called projects that is an array of 2 other objects. Each one of the other 2 objects has an array of images. I was able to ...
Ahmed Magdy's user avatar
-1 votes
1 answer
64 views

Javascript Function Understanding

I am currently working on javascript function but these programe make me little bit confuse kindly please explain whole about the program given below, confusing towards for loop decrement. var ...
Haseeb Jumani's user avatar
3 votes
3 answers
2k views

accessing nested objects with a for in loop

Within javascript, I am trying to print the values from a few objects listed in an array in an object. The question has to do with drilling into objects/arrays with a for in loop. More specifically I'...
maftoun95's user avatar
0 votes
1 answer
239 views

Swift C-style loop [duplicate]

for (var i = 1; i < 1024; i *= 2) { print(i) } How can this be done with for in loop? The given solution is for += operator not *= operator. Please provide a solution for *= thanks.
Ghulam Ahmed's user avatar

1
4 5
6
7 8
13