for-in-loop

Using delay DispatchQueue in “for in loop”

别来无恙 提交于 2021-02-11 14:08:29
问题 The task is to change the background color once a second. Was used "for in loop". For delay, a DispatchQueue was used. Everything seems to be fine, but it was noticed that after 10 iterations, the background color begins to change with a delay of 2 seconds, a little later in 3 seconds. The more iterations, the greater the delay. I displayed time in the console (seconds) to see how it changes. I see the results, but I do not understand what is wrong. I did the task through a timer, there were

addEventListener works in simple for loop but doesn't work with for-in loop

一笑奈何 提交于 2021-01-28 05:40:38
问题 When I use simple for loop, addEventListener works well in for loop. But when I use for-in loop, it makes error like Uncaught TypeError: checklist[i].addEventListener is not a function This is my work-well code. var checklist = document.querySelectorAll(".checklist"); for (var i = 0, len = checklist.length; i < len; i += 1) { checklist[i].addEventListener('change', function (event) { alert('test'); }); } This is my Error code. var checklist = document.querySelectorAll(".checklist"); for (var

Typescript element implicitly has type any with for…in loops

喜夏-厌秋 提交于 2021-01-20 12:12:29
问题 I have a JSON object imported from a JSON file (with resolveJsonModule: true ). The object looks like this: "myobject": { "prop1": "foo", "prop2": "bar" } and it's type therefore looks like this: myobject: { prop1: string, prop2: string } That's very nice but when I try to use a for...in loop, for (const key in myobject) { console.log(myobject[key]) } I get this error: TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ "prop1":

Typescript element implicitly has type any with for…in loops

Deadly 提交于 2021-01-20 12:12:08
问题 I have a JSON object imported from a JSON file (with resolveJsonModule: true ). The object looks like this: "myobject": { "prop1": "foo", "prop2": "bar" } and it's type therefore looks like this: myobject: { prop1: string, prop2: string } That's very nice but when I try to use a for...in loop, for (const key in myobject) { console.log(myobject[key]) } I get this error: TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ "prop1":

what is for…in statement in javascript

≡放荡痞女 提交于 2020-02-24 05:44:30
问题 anyone can explain how to use for...in statement in javascript. I had read the w3school article but i think it is not so clear.Below is the code, please explain this: <html> <body> <script type="text/javascript"> var x; var mycars = new Array(); mycars[10] = "Saab"; mycars[20] = "Volvo"; mycars[30] = "BMW"; for (x in mycars) { document.write(mycars[x] + "<br />"); } </script> </body> </html> 回答1: A for in loop will iterate through every property in an object. In your example, the x variable

difference between for loop and for-in loop in javascript

五迷三道 提交于 2020-01-04 06:22:28
问题 I found that there is a difference between for loop and for-in loop in javascript. When I define a new array: var a=new Array(); Then I put some value into in but not contiguously for example: a[0]=0;a[1]=1;a[4]=4; When I use for(i=0;i<5;i++) to get the value and use alert to show it, it's different from using for(i in a) . The previous one will show elements in index 2,3 which shows "undefined" while for-in will show only index 0,1,and 4. Can anybody tell me why? 回答1: for (... in ...) is

Javascript: Why use a for loop instead of a for-in loop for arrays?

安稳与你 提交于 2020-01-04 04:14:07
问题 I have been reading Object-Oriented Javascript by Stoyan Stefanov, and at one point he writes: The for-in loop is used to iterate over the element of an array (or an object, as we'll see later). This is it's only use; it can not be used as a general-purpose repetition mechanism that replaces for or while . Let's see an example of using a for-in to loop through the elements of an array. But bear in mind that this is for informational purposes only, as for-in is mostly suitable for objects, and

How to show message in for-loop issues in javascript or using jquery

╄→尐↘猪︶ㄣ 提交于 2020-01-03 06:37:39
问题 I tried this for almost two days but still nothing. Maybe someone can help who is highly skilled in javascript loops. I have this code: $(function(){ var len = $('#groupContainer > div').length; var arr = []; for(var i=0; i < len; i++){ var number = $('#number_' + [i + 1]); var date = $('#date_' + [i + 1]); var count = i + 1; var message =""; console.log(number) var a = number.map(function(){ return this.value; }); var b = date.map(function(){ return this.value; }); var newObj = {number: a[0]

Problems with JavaScript “for in” loop

Deadly 提交于 2019-12-31 07:44:06
问题 I have an array of objects which will be the basis for a certain menu in my website. It will be build using JavaScript: [ {"menuName":"Contact Info","sectionName":"contacts"}, {"menuName":"Facilities","sectionName":"facilities"}, {"menuName":"Locations","sectionName":"locations"}, {"menuName":"Packages","sectionName":"packages"}, {"menuName":"Policies","sectionName":"policies"}, {"menuName":"Reviews","sectionName":"reviews"}, {"menuName":"Rooms","sectionName":"rooms"} ] So I decided to use

With fast enumeration and an NSDictionary, iterating in the order of the keys is not guaranteed – how can I make it so it IS in order?

元气小坏坏 提交于 2019-12-30 04:00:10
问题 I'm communicating with an API that sends back an NSDictionary as a response with data my app needs (the data is basically a feed). This data is sorted by newest to oldest, with the newest items at the front of the NSDictionary. When I fast enumerate through them with for (NSString *key in articles) { ... } the order is seemingly random, and thus the order I operate on them isn't in order from newest to oldest, like I want it to be, but completely random instead. I've read up, and when using