Function Node Programming-Arrays,Objects and Loops

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

Using Loops

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.

This 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- Arrays and Objects

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

Questions and Answers-Loops

  1. 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

<–Function Node Programming Part1- Variables

Related resources and tutorials:

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 *