Getting value via getNodeParameter

Greetings!
I’m in the process of making a node that takes a .json-file from another node and calculates the average of the included numbers.

async execute (this: IExecuteFunctions): Promise<INodeExecutionData[][]>{
        const array = this.getInputData();
        const arraySize = array.length as number;
        let average : number;
        let sum : number = 0;
    
        for (let i = 0; i< arraySize; i++){
                sum += this.getNodeParameter('value', i) as number;
                console.log(array[i]+" "+ sum);
            };
            average = sum/arraySize;
   
            let resultMessage: IDataObject = { "value": average };
            let resultItem: INodeExecutionData = { json: resultMessage };
            return [[resultItem]];  
    }

The input looks like this:

[{    json: {     "value": '2'    }  },    {    json: {      "value": '3'    }  }]

If I run the node in a workflow however the function doesn’t seem to get the value of the json array.
I’m pretty sure I just don’t get how getNodeParameter works.
Could you please give me some input?

Hi @Maxleton, first of all welcome to the community!

getNodeParameter would return to the value of a node parameter but from your description I understand you want to access the input items, is that correct?

If so, you could base your logic on your array constant rather than using getNodeParameter. E.g. like so:

async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
    const array = this.getInputData();
    const arraySize = array.length as number;
    let average : number;
    let sum : number = 0;

    for (let i = 0; i< arraySize; i++){
        sum += array[i].json.value as number;
        console.log(array[i]+" "+ sum);
    };
    average = sum/arraySize;
    let resultMessage: IDataObject = { "value": average };
    let resultItem: INodeExecutionData = { json: resultMessage };
    return [[resultItem]];
}

For an input like so:

The above code would return a single item looking like this:

Is this what you had in mind?

1 Like

Yes!
Thank you, that is exactly what I’ve been looking for :]

1 Like

Hi @MutedJam
I am trying to use this code to create an average for the column “rating” and again for the “calc_sentiment” column, but struggling as this level of coding is way about my pay grade!
Is there any chance you can help?
Thanks - Jack

Hi @Jack

Welcome to the community!

Seems like you are trying to set some values.
Have a look at the Set node. This might be better for your usecase. :wink:

Hi @BramKn
I put the Function node in to show the data I am trying to get an average for.
Thanks - Jack

Ah sorry @Jack
Didn’t read the whole post.
try this:

1 Like

@BramKn
Thank you very much - your code worked a treat!

For future reference, I ran it through askjarvis.io to help me understand a bit more:

  1. Create two empty arrays to hold the rating and calc_sentiment values.
  2. Loop through the items array and push the rating and calc_sentiment values into the arrays.
  3. Calculate the average rating and calc_sentiment values by adding all the values in the array and dividing by the number of values in the array.
  4. Assign the average rating and calc_sentiment values to the first item in the items array.
  5. Return the items array.
2 Likes