Need help with comparing 2 csv-files (2 nodes) within a function

Hello guys,

I'm new here(I'm a noob haha), so I don't really know how the different nodes work.

My problem is:
I'd like to get the difference between 2 csv-files which are both returned as arrays. So i have got 2 input nodes as csv-files and I don't know how to subtract one from another and return it as a new array in the msg.payload.
How can i write a function which calculates the difference between them?
Can anyone help me with that? Or is that even possible what i want to do?
Thanks!

Define what you mean by differences between them. Do you mean you want to subtract the values in one from the values in the other?
Are they single dimensional csv files (so just a sequence of numbers)?

I have returned those 2 arrays in my current flow and i want to subtract each value from the first array from each value from the second array, e.g. 20-15; 20-35; 20-46,25; 20-15; etc.
And the result should be returned as a new array^^

The first thing you will have to do is to combine them into a single message, as you can only access the current message in a node (unless you have previously saved some data). You can do this using the Join node to Combine Each msg.payload into a key/value pair using the topic as the key. You will have to make sure the two messages have different topics and feed them into the join node. Have a play with that, feeding the output of the Join node into a debug node to check it is correct. Then you should see the two arrays as msg.payload.topic1 and msg.payload.topic2 (or whatever topics you have defined. Then you can use a function node to run through the array doing something like (untested and probably full of syntax an logic errors).

let arr1 = msg.payload.topic1;
let arr2 = msg.payload.topic2;
let answer = [];
for (let i = 0; i <arr1.length; i++) {
  answer[i] = [];
  for (let j = 0; j<arr1[i].length; i++) {
    answer[i][j] = arr1[i][j] - arr2[i][j];
  }
}
msg,payload = answer;
return msg;

Note that this will fail if the input arrays are not the same structure, so whether you will need to check that first I don't know.

1 Like

thanks a lot! :slight_smile: , I'm so sorry but can you explain how to use the join node? Because I don't really get what i have to fill in in that node. Where do i define topic1 and topic2?

For the join node select Manual then set it up as I described. You don't define topic1 and topic2 in the join node, it just expects the two messages to have different topics. If they have not already got different topics then put Change nodes in each path configured to Set msg.topic To topic1 and topic2, or probably in fact more meaningful names that indicate what is in the message.
This might be helpful, also of course all the other node red documentation.
https://nodered.org/docs/user-guide/messages

1 Like

As an alternative to a function, you should also be able to do this with a change node using JSONata. That would probably be faster to execute but not enough to worry about unless you have to do lots of these fairly quickly.

You might also need to think about how big your files are.

How to put a loop in function so that it compares the value in .csv file to the master file which is in our server?
in simple words -

If (imei of .csv file = imei of master file)
{ change the asset value}
else if (imei of .csv file != imei of master file)
{add new row of the IMEI and the asset value}
else { NO CHANGE}

Please advise.