Hi everyone!
Some problems with my node code This is what i want to do:
I have a json with one item, which is the union of two merged inputs, so, i would like to compare these two arrays: “taskAssignedNotion” and "taskAssignedBrevo. The output should be:
if they are equals → one new field “taskequals = true”;
if they are not equals → the element of array that change from one to another in the array where it is.
Thank you to everyone who can help me
What is the error message (if any)?
ERROR: Code doesn’t return items properly
Please return an array of objects, one for each item you would like to output.
Please share your workflow
The code below only includes the option of ‘if they are the same’, maybe you can advise me how to do the other options as well
JSON INPUT
[
{
"TaskAssignedBrevo": [
"313123-b57e-4505-b091-c3b9d312d39d16",
"024d2094-f5a6-4255-83df-064ca4234faf4f",
"7e0292e7-43-4637-a1e3-123213dfs",
"5e36c31c-f44259-4b44-995b-41f1d516b101"
],
"id_brevo": 19,
"email": "test@examplecom",
"TaskAssignedNotion": [
"d44243d9d-b57e-45r5-b091-c3b9d23416",
"024d2094-f5a6-4255-83df-064c324faf4f",
"7e0292e7-fb22-4637-a1e3-ba5dfs0ac3f6",
"5e36c31c-f459-4b44-995b-41243516b101"
],
"name": "Test 1",
"id_page": "998r42ea7-c8ff-42af-a190-a3d504dfd769"
}
]
Information on your n8n setup
n8n version:1.3.1
Database (default: SQLite):default
n8n EXECUTIONS_PROCESS setting (default: own, main):default
Running n8n via (Docker, npm, n8n cloud, desktop app):n8n cloud
Operating system:windows
Hi @LucaDipa Does this do what you’re expecting?
const taskBrevo = $('Code1').all()[0].json.TaskAssignedBrevo
const taskNotion = $('Code1').all()[0].json.TaskAssignedNotion
function compareTasksAndReturnOutput(taskBrevo, taskNotion) {
const differentTasks = taskBrevo.filter(task => !taskNotion.includes(task));
if (differentTasks.length === 0) {
return { taskequals: true };
} else {
return { changedTask: differentTasks[0] };
}
}
const output = compareTasksAndReturnOutput(taskBrevo, taskNotion);
return output;
With regards to your second part of what you’re needing, I believe this code will output what you’re looking for if they’re changed, but let me know if I’m off base:
const taskBrevo = $('Code1').all()[0].json.TaskAssignedBrevo
const taskNotion =$('Code1').all()[0].json.TaskAssignedNotion
function compareTasksAndReturnOutput(taskBrevo, taskNotion) {
const differentTasks = taskBrevo.filter(task => !taskNotion.includes(task));
const changedTasks = taskBrevo.filter(task => differentTasks.includes(task));
if (differentTasks.length === 0) {
return [{ taskequals: true }];
} else {
return [{ changedTasks }];
}
}
const output = compareTasksAndReturnOutput(taskBrevo, taskNotion);
return output;
Hi @EmeraldHerald ,
thanks for your reply!
I tried the second code, but it does not seem to work properly. Because if you notice in these two fields:
[
{
"TaskAssignedBrevo": [
"d4903d9d-b57e-4505-b091-c3b9dad39d16",
"024d2094-f5a6-4255-83df-064ca31faf4f",
"7e0292e7-fb22-4637-a1e3-ba53a40ac3f6"
],
"TaskAssignedNotion": [
"d4903d9d-b57e-4505-b091-c3b9dad39d16",
"024d2094-f5a6-4255-83df-064ca31faf4f",
"7e0292e7-fb22-4637-a1e3-ba53a40ac3f6",
"5e36c31c-f459-4b44-995b-41f1d516b101"
]
}
]
there is an extra task in “TaskAssignedNotion”, so as an output it should give me that task similar to this:
"TaskAssignedNotion": [
"5e36c31c-f459-4b44-995b-41f1d516b101"
]
But the result it gives me is that they are equals…
Hey @EmeraldHerald , i found a solution!
this is the code version:
const taskBrevo = $('Merge: taskNotion + taskBrevo').all()[0].json.TaskAssignedBrevo;
const taskNotion = $('Merge: taskNotion + taskBrevo').all()[0].json.TaskAssignedNotion;
function compareTasksAndReturnOutput(taskBrevo, taskNotion) {
const missingInNotion = taskBrevo.filter(task => !taskNotion.includes(task));
const missingInBrevo = taskNotion.filter(task => !taskBrevo.includes(task));
const output = [];
if (missingInNotion.length > 0) {
output.push({
source: "TaskAssignedBrevo",
missingTasks: missingInNotion
});
}
if (missingInBrevo.length > 0) {
output.push({
source: "TaskAssignedNotion",
missingTasks: missingInBrevo
});
}
if (output.length === 0) {
output.push({
taskequals: true
});
}
return output;
}
const output = compareTasksAndReturnOutput(taskBrevo, taskNotion);
return output;
I hope this code will also be of use to other people!
2 Likes
Thanks so much for posting your solution @LucaDipa !
1 Like
system
Closed
September 5, 2023, 10:53am
6
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.