Loops let you repeat an action.
There are several loops available they are for,while, do while.
By far the most common one is the for loop. The general format is:
for (begin; condition; step) { // ... loop body ... }
Example use:
let count=5 for(let x=0;x<count;x++) { node.log("count=" + x); }
This will print the value of count from 0 to 4.
Very often we will need to send messages in the for loop to do this we use the node.send(msg) method
let count=5 for(let x=0;x<count;x++) { msg.payload=x; node.send(msg); }
Looping Through an Array
let names= [Steve;John.Jane,Jill]; for (let i=0;i<names.length;i++) { node.log("name = "+names[i]; }
Using for…of.
This was actually new to me but is very useful to know
const fruits = ['apples', 'pears', 'oranges']; for (const fruit of fruits) { node.log(fruit); } // Expected output: "apples" // Expected output: "pears" // Expected output: "oranges"
Looping Through Objects
There are two common methods.
Ths first uses the for property in object syntax as shown below:
const object = { a: 1, b: 2, c: 3 }; for (const property in object) { node.log(property + ":"+ object[property]); } // Expected output: // "a: 1" // "b: 2" // "c: 3"
We can also get the keys of an object and loop through the object using the keys.
The first step is to get the keys using the Object.keys() function. This returns an array of keys.
let data={"voltage":100,current:2}; let keys=Object.keys(data); //return array of keys for (let i=0;i<keys.length;i++) { let key=keys[i]; node.log("key = "+ key + " and value= " + data[key]); }
Continue and Break
The continue statement is used to advance the loop by 1 element and the break is used to quit the loop.
Using continue.
const fruits = ['apples', 'pears', 'oranges']; for (const fruit of fruits) { if (fruit=="pears") continue; else node.log(fruit); } // Expected output: "apples" // Expected output: "oranges"
Using break
const fruits = ['apples', 'pears', 'oranges']; for (const fruit of fruits) { if (fruit=="pears") break; else node.log(fruit); } // Expected output: "apples"
Multiple Variables
You can initialise and increment multiple variables as shown below. However there can only be one condition.
for (let x = 0, y = 0; x < 2; x++, y++) { node.log("variable x: " + x); node.log("variable y: " + y); } // Expected output: 0,1
The Step or Increment operator
Returning to our general for loop:
for (begin; condition; step) { // ... loop body ... }
The most common step is to increment by 1 and so we have the syntax x++.
However we could increment by any number and also decrement.
so x– would decrement the count and x=x+5 would increment the count by 5.
Questions and Answers
- Given the code:
for (let x = 0; x < 21; x=x+5) { node.log("variable x: " + x); }
What is the expected result
2. Given the code:
for (let x = 0; x < 20; x=x+5) { node.log("variable x: " + x); }
What is the expected result?
3. Given the code:
const fruits = ['apples', 'pears', 'oranges']; for (const fruit of fruits) { if (fruit == "oranges") continue; else node.log(fruit); } return msg;
What is the expected result?
Answers
1.0,5,10,15,20
2.0,5,10,15
3.apples,pears
Related Tutorials and Resources: