JS String Comparison

Hi!

I’ve got a bit of script running in a function node, and I can;t for the life of me work out what is tripping it up:

const a1= items[0].json.Description; 
const a2= items[1].json.Description;
const L = a1.length;
var i= 0;
while(i<L && a1.charAt(i)=== a2.charAt(i)) i++;

return a1.substring(0, i);

I’m getting the error:

executionData.every is not a function

Basically comparing two strings, and taking the common portion. Can anyone help?

Function-Nodes should return an array of objects. You can find the documentation here:
https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.function/

So to make it work you can change the last line to the following:

return [
  {
    json: {
      text: a1.substring(0, i),
    }
  }
];
1 Like

Perfect, thanks Jan.

Glad that it was helpful. Have fun!