Function Node Programming-Loops,Arrays and objects

Using Loops

There are several loops available – for,while, do while. Loops let you repeat an action.

By far the most common one is the for loop. The general format is:

for (begin; condition; step) {
// ... loop body ...
}





Example use:
for(let x=0;x<count;x++)
{
//code here
}

reference

for…while

JavaScript Objects and Arrays

To work with node-red function node you will need a basic understanding of JavaScript objects and arrays.

An object is a collection of key value pairs separated by a comma and enclosed in braces ({})

Example:

let data={"voltage":100,current:2};

Note that the key doesn’t need to be in quotes but it  often is.

An array is simple a list with each item separated by a comma and enclosed in square brackets.
Example:

let data=["voltage",100,"current",2];

Notice that is can be mixed strings and numbers and the strings must be in quotes.

Accessing object properties

To access the current in:

let data={"voltage":100,current:2};

we can use either of the formats shown below:

let current =data.current;
let current=data["current"];

Note if the key is a variable then we need to use the square bracket format.

let current=data[current];

For an array we need to use the index which by convention starts at index 0.

So to get the current value we use:

let current=data[3]; //=2

Looping Through an Array

let names= [Steve;John.Jane,Jill];

for (let i=0;i<names.length;i++)
{
node.log("name = "+names[i];
}

Looping Through an Object

Getting the keys of an object and looping through the object is quite a common task.

The first step is to get the keys using the Object.keys() function.

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]);
 }

You can also loop using for in syntax which can be used for objects and arrays..

let data = { "voltage": 100, current: 2 };
for (let key in data)
{
 node.log("key ="+key);
node.log("value ="+data[key]);
}

 

let array = [14, 6, 7];
for (let index in array)
    node.log("value= " + array[index]);

Questions and Answers

Q1 What would the following code produce as output:

let data = { "voltage": 100, current: 2 };
for (let key in data)
{
node.log("key ="+key);
}

Q2 what would the following code produce as output:

let array = [14, 6, 7];
for (let index in array)     node.log("value= " + index);

Answers

Q1 – voltage,current

Q2 -0,1,2

Related Tutorials and Resources:

Node-red Function Node

Click to rate this post!
[Total: 0 Average: 0]

2 comments

  1. Hi Steve,
    I think in your example, JavaScript Objects and Arrays, there should be [] and not {}
    as stated:
    An array is simple a list with each item separated by a comma and enclosed in square brackets.
    Example:
    let data={“voltage”,100,”current”,2};

    I hope you don’t mind.
    Greetings and take good care
    Heinz

Leave a Reply

Your email address will not be published. Required fields are marked *