# Node code: Compare two arrays

**URL:** <https://community.n8n.io/t/node-code-compare-two-arrays/29764>\
**Category:** Questions\
**Tags:** code\
**Created:** [August 25, 2023, 12:38pm UTC](https://community.n8n.io/t/node-code-compare-two-arrays/29764 "2023-08-25T12:38:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![LucaDipa](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/lucadipa/32/25841_2.png) [@LucaDipa](https://community.n8n.io/u/LucaDipa)\
**Post date:** [August 25, 2023, 12:38pm UTC](https://community.n8n.io/t/node-code-compare-two-arrays/29764/1 "2023-08-25T12:38:53Z")

</div>

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

```auto
[
  {
    "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**

---

<div class="post-metadata">

**Author:** ![EmeraldHerald](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/emeraldherald/32/35355_2.png) [@EmeraldHerald](https://community.n8n.io/u/EmeraldHerald)\
**Post date:** [August 25, 2023, 3:21pm UTC](https://community.n8n.io/t/node-code-compare-two-arrays/29764/2 "2023-08-25T15:21:02Z")

</div>

Hi @LucaDipa 👋 Does this do what you’re expecting?

```auto
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:

```auto
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;

```

---

<div class="post-metadata">

**Author:** ![LucaDipa](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/lucadipa/32/25841_2.png) [@LucaDipa](https://community.n8n.io/u/LucaDipa)\
**Post date:** [August 28, 2023, 7:52am UTC](https://community.n8n.io/t/node-code-compare-two-arrays/29764/3 "2023-08-28T07:52:10Z")

</div>

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:

```auto
[
  {
    "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:

```auto
"TaskAssignedNotion": [
      "5e36c31c-f459-4b44-995b-41f1d516b101"
    ]

```

But the result it gives me is that they are equals… ☹

---

<div class="post-metadata">

**Author:** ![LucaDipa](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/lucadipa/32/25841_2.png) [@LucaDipa](https://community.n8n.io/u/LucaDipa)\
**Post date:** [August 29, 2023, 10:52am UTC](https://community.n8n.io/t/node-code-compare-two-arrays/29764/4 "2023-08-29T10:52:30Z")

</div>

Hey @EmeraldHerald, i found a solution!

this is the code version:

```auto
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!

---

<div class="post-metadata">

**Author:** ![EmeraldHerald](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/emeraldherald/32/35355_2.png) [@EmeraldHerald](https://community.n8n.io/u/EmeraldHerald)\
**Post date:** [August 29, 2023, 10:53am UTC](https://community.n8n.io/t/node-code-compare-two-arrays/29764/5 "2023-08-29T10:53:01Z")

</div>

Thanks so much for posting your solution @LucaDipa !

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/system/32/195824_2.png) [@system](https://community.n8n.io/u/system)\
**Post date:** [September 5, 2023, 10:53am UTC](https://community.n8n.io/t/node-code-compare-two-arrays/29764/6 "2023-09-05T10:53:30Z")

</div>

Closed on September 5, 2023, 10:53am UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
