---
url: https://gudhub.com/docs/utils/compare-items/
title: "Compare Items\n                #"
description: "compareItems is the method that compares items by their fields and item IDs. Namely, it compares source items or fields with destination ones. Then it adds sour"
lang: uk
updated: 2026-09-17T14:12:47.171Z
---

# Compare Items [#](https://gudhub.com/docs/utils/compare-items/#compare-items)

**compareItems** is the method that compares items by their fields and item IDs. Namely, it compares source items or fields with destination ones. Then it adds source items to three different arrays based on the comparison results.

While invoking, compareItems takes three arguments:

| Name | Type | Description |
| :-- | :-- | :-- |
| sorceItems | `array` | _array of items that is going to be used as a source for comparison_ |
| destinationItems | `array` | _array of items that is gong to be compared with sorceItems_ |
| fieldToCompare | `number` | _ID of the field by which items can be compared_ |

> The `fieldToCompare` argument is **not a mandatory one**. It can be used when you need to compare only certain fields of different items.

This method can compares items in two ways:

-   by items IDs
-   by field ID

**Comparison by field ID** will be performed by values from the entered field ID. **In other case**, the comparison will be performed for all fields that contain both items

The method returns the object that contains all those arrays:

Copy

```json
{
    "is_items_diff": false,
    "new_src_items":[],
    "diff_src_items":[],
    "same_items":[]
}
```

| Name | Type | Description |
| :-- | :-- | :-- |
| is\_items\_diff | `boolean` | _shows is there different items or not_ |
| new\_src\_items | `array` | _contains items with different IDs; [example](https://gudhub.com/docs/utils/compare-items/#different-ids)_ |
| diff\_src\_items | `array` | _contains items with same IDs but different fields; [example](https://gudhub.com/docs/utils/compare-items/#same-ids-but-different-fields-values)_ |
| same\_items | `array` | _contains items with same IDs and same fields; [example](https://gudhub.com/docs/utils/compare-items/#perfect-match)_ |

> If source items have the same fields with the same values as destination items they will be described as same ones, even if `destinationItems` have additional fields those `sorceItems` does not have.

## Using fieldToCompare [#](https://gudhub.com/docs/utils/compare-items/#using-fieldtocompare)

When we pass field ID into `fieldToCompare`, the method will compare items by it, not by the whole item:

Copy

```js
import GudHub from '@gudhub/core';
const gudhub = await new GudHub();
const FIELDID = 645887;

let sorceItems = [{
    "item_id": 2987986,
    "fields": [{
      "field_id": 645887,
      "field_value": "Dow",
      },
      {
        "field_id": 645888,
        "field_value": "Johana",
        }]
}
];

let destinationItems = [{
  "item_id": 2987996,
  "status": 3,
  "fields": [{
      "field_id": 645887,
      "field_value": "Dow",
      },
      {
        "field_id": 645888,
        "field_value": "John",
        }]
  }
];

let comparedItems = gudhub.compareItems(sorceItems, destinationItems, FIELDID);

console.log(comparedItems);
```

Apart from replacement of item ID to field value, operating logic of comparison does not change:

Copy

```json
{
  "is_items_diff": true,
  "new_src_items": [],
  "diff_src_items": [{
      "item_id": 2987986,
      "fields": [{
          "field_id": 645887,
          "field_value": "Dow",
          },
          {
          "field_id": 645888,
          "field_value": "Johana",
      }] 
  }],
  "same_items": []
}
```

## Examples [#](https://gudhub.com/docs/utils/compare-items/#examples)

We have prepared the examples for four comparison cases:

### Perfect Match [#](https://gudhub.com/docs/utils/compare-items/#perfect-match)

This is a situation when 'sorceItems' contains the same item with the one from 'destinationItems':

Copy

```js
import GudHub from '@gudhub/core';
const gudhub = await new GudHub();

let sorceItems = [{
    "item_id": 2987996,
    "fields": [{
      "field_id": 645887,
      "field_value": "Dow",
      },
      {
        "field_id": 645888,
        "field_value": "John",
        }]
  }
]

let destinationItems = [{
  "item_id": 2987996,
  "fields": [{
      "field_id": 645887,
      "field_value": "Dow",
      },
      {
        "field_id": 645888,
        "field_value": "John",
        }]
}]


let comparedItems = gudhub.compareItems(sorceItems, destinationItems)

console.log(comparedItems)
```

It will return:

Copy

```json
{
    "is_items_diff": false,
    "new_src_items": [],
    "diff_src_items": [],
    "same_items": [{ 
        "item_id": 2987996, 
        "fields": [{
            "field_id": 645887,
            "field_value": "Dow",
            },
            {
            "field_id": 645888,
            "field_value": "John",
            }]
        }]
}
```

### Different Number of Fields [#](https://gudhub.com/docs/utils/compare-items/#different-number-of-fields)

The 'sorceItems' item has less fields, but all of them are the same with the destinationItems item:

Copy

```js
import GudHub from '@gudhub/core';
const gudhub = await new GudHub();

let sorceItems = [{
    "item_id": 2987996,
    "fields": [{
      "field_id": 645887,
      "field_value": "Dow",
    }]
}]

let destinationItems = [{
  "item_id": 2987996,
  "fields": [{
      "field_id": 645887,
      "field_value": "Dow",
      },
      {
        "field_id": 645888,
        "field_value": "John",
        }]
}]


let comparedItems = gudhub.compareItems(sorceItems, destinationItems)

console.log(comparedItems)
```

As a result, we get:

Copy

```json
{
  "is_items_diff": false,
  "new_src_items": [],
  "diff_src_items": [],
  "same_items": [{ 
    "item_id": 2987996, 
    "fields": [{
        "field_id": 645887,
        "field_value": "Dow",
    }]
  }]
}
```

### Different IDs [#](https://gudhub.com/docs/utils/compare-items/#different-ids)

In this case, items have different IDs:

Copy

```js
import GudHub from '@gudhub/core';
const gudhub = await new GudHub();

let sorceItems = [{
    "item_id": 2987986,
    "fields": [{
        "field_id": 645887,
        "field_value": "Mikelson",
        },
        {
        "field_id": 645889,
        "field_value": "Johana",
    }]
}]

let destinationItems = [{
  "item_id": 2987996,
  "fields": [{
      "field_id": 645887,
      "field_value": "Dow",
      },
      {
        "field_id": 645888,
        "field_value": "John",
        }]
}]


let comparedItems = gudhub.compareItems(sorceItems, destinationItems)

console.log(comparedItems)
```

As a result, source item is added to new\_src\_items array:

Copy

```json
{
    "is_items_diff": true,
    "new_src_items": [{
        "item_id": 2987986, 
        "fields": [{
            "field_id": 645887,
            "field_value": "Mikelson",
            },
            {
            "field_id": 645889,
            "field_value": "Johana",
            }]
    }],
    "diff_src_items": [],
    "same_items": []
}
```

### Same IDs but Different Fields Values [#](https://gudhub.com/docs/utils/compare-items/#same-ids-but-different-fields-values)

In this example, items of both arrays have same IDs:

Copy

```js
import GudHub from '@gudhub/core';
const gudhub = await new GudHub();

let sorceItems = [{
    "item_id": 2987996,
    "status": 1,
    "fields": [{
        "field_id": 645887,
        "field_value": "Dow",
        },
        {
          "field_id": 645888,
          "field_value": "Johana",
          }]
}]

let destinationItems = [{
  "item_id": 2987996,
  "fields": [{
      "field_id": 645887,
      "field_value": "Dow",
      },
      {
        "field_id": 645888,
        "field_value": "John",
        }]
}]


let comparedItems = gudhub.compareItems(sorceItems, destinationItems)

console.log(comparedItems)
```

The following object will be a result:

Copy

```json
{
    "is_items_diff": true,
    "new_src_items": [],
    "diff_src_items": [{
        "item_id": 2987996, 
        "fields": [{
            "field_id": 645887,
            "field_value": "Dow",
            },
            {
            "field_id": 645888,
            "field_value": "Johana",
            }]
        }],
    "same_items": []
}
```
