---
url: https://gudhub.com/uk/docs/utils/populate-items/
title: "Populate Items\n                #"
description: "This is the method that allows to populate items and to update the item with data from another one. In addition, it significantly optimize the work with the ser"
lang: uk
updated: 2026-09-12T04:01:27.898Z
---

# Populate Items [#](https://gudhub.com/uk/docs/utils/populate-items/#populate-items)

This is the method that allows to populate items and to update the item with data from another one. In addition, it significantly optimize the work with the server.

| Name | Type | Description |
| :-- | :-- | :-- |
| items | `array` | _array of items that will be upgraded_ |
| model | `array` | _item model by that items are updated_ |
| keep\_data | `boolean` | _optional argument that shows whether fields, that will not be updated, will be erased_ |

## model [#](https://gudhub.com/uk/docs/utils/populate-items/#model)

**model** is an array of objects that contains source data for updating item fields. It has such view:

Копіювати

```json
model = [{      
    "field_id" : 228464,
    "field_value" : "example 1"
    },
    {      
    "field_id" : 228464,
    "field_value" : "example 2"
    }]
```

Each object contains two properties:

| Name | Type | Description |
| :-- | :-- | :-- |
| field\_id | `number` | _ID by that the fields will be updated_ |
| field\_value | `string` | _value to be added to a certain field instead of the existing one_ |

## keep\_data [#](https://gudhub.com/uk/docs/utils/populate-items/#keep-data)

Thanks to **keep\_data** we can adjust the request for the faster and optimal work. The default value is false.

If its value is true, all fields, even those without changes, will be sent to the server. As opposite, if value is false, unchanged fields will be removed from the request. Server will not process extra operations. So, we can regulate the amount of work that we send to the server.

## Example [#](https://gudhub.com/uk/docs/utils/populate-items/#example)

Depending on the value of keep\_data, the method will have different objects in the result array.

### keep\_data is false [#](https://gudhub.com/uk/docs/utils/populate-items/#keep-data-is-false)

In cases when the keep\_data is false or have no value:

Копіювати

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

let items = [{
    "item_id": 2987986,
    "fields": [{
        "field_id": 645884,
        "field_value": "Dow",
        },
        {
        "field_id": 645888,
        "field_value": "John",
        },
        {
        "field_id": 645889,
        "field_value": "384595933",
        }],
    },
    {
    "item_id": 2987996,
    "fields": [{
        "field_id": 645887,
        "field_value": "Morison",
        },
        {
            "field_id": 645888,
            "field_value": "Johana",
        },
        {
            "field_id": 645889,
            "field_value": "2343588",
        }]
    }
]

let model = [{
  "field_id": 645887,
  "field_value": "Mikelson",
  },
  {
  "field_id": 645888,
  "field_value": "Jonny",
  }
]


// populate items
let populatedItems = gudhub.populateItems(items, model)

// displayed full object
populatedItems.forEach(item => {
  console.log("item_id:", item.item_id)
  console.log(item.fields)
});
```

Then all fields that have not been changed are deleted or replaced.

Копіювати

```json
[{
    "item_id": 2987986,
    "fields":[{ 
        "field_id": 645887, 
        "field_value": "Mikelson" 
        },
        { 
        "field_id": 645888, 
        "field_value": "Jonny" 
        }
    ]
},
{
    "item_id": 2987996,
    "fields":[{ 
        "field_id": 645887, 
        "field_value": "Mikelson" 
        },
        { 
        "field_id": 645888, 
        "field_value": "Jonny" 
        }
    ]
}]
```

### keep\_data is true [#](https://gudhub.com/uk/docs/utils/populate-items/#keep-data-is-true)

If keep\_data is true, then all unchanged data is kept in the objects, and all fields that were not in the original element are added to it:

Копіювати

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

let items = [{
    "item_id": 2987986,
    "fields": [{
        "field_id": 645884,
        "field_value": "Dow",
        },
        {
        "field_id": 645888,
        "field_value": "John",
        },
        {
        "field_id": 645889,
        "field_value": "384595933",
        }],
    },
    {
    "item_id": 2987996,
    "fields": [{
        "field_id": 645887,
        "field_value": "Morison",
        },
        {
            "field_id": 645888,
            "field_value": "Johana",
        },
        {
            "field_id": 645889,
            "field_value": "2343588",
        }]
    }
]

let model = [{
  "field_id": 645887,
  "field_value": "Mikelson",
  },
  {
  "field_id": 645888,
  "field_value": "Jonny",
  }
]


// populate items
let populatedItems = gudhub.populateItems(items, model, true)

// displayed full object
populatedItems.forEach(item => {
  console.log("item_id:", item.item_id)
  console.log(item.fields)
});
```

As you can see, the matching fields are updated and the new field is added to the first item, but all source fields are retained:

Копіювати

```json
[{
    "item_id": 2987986,
    "fields":[{ 
        "field_id": 645884, 
        "field_value": "Dow" 
        },
        { 
        "field_id": 645888, 
        "field_value": "Jonny" 
        },
        {
        "field_id": 645889, 
        "field_value": "384595933" 
        },
        { 
        "field_id": 645887, 
        "field_value": "Mikelson"
        }
    ]
},
{
    "item_id": 2987996,
    "fields":[{ 
        "field_id": 645887, 
        "field_value": "Mikelson" 
        },
        { 
        "field_id": 645888, 
        "field_value": "Jonny" 
        },
        { 
        "field_id": 645889, 
        "field_value": "2343588" 
        }
    ]
}]
```
