---
title: "MapModel"
---
Defined in: [api/node/typescript/models.ts:554](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L554)

MapModel provides rows that are generated by a map function based on the
rows of another model.

## Extends

- `NativeAdapterModel`\<`U`\>

## Type Parameters

### T

`T`

item type of source model that is mapped to U.

### U

`U`

the type of the mapped items.

### Example

Here we have an [ArrayModel](/latest/docs/node/api/classes/arraymodel/) holding rows of a custom interface `Name`
and a MapModel that maps the name rows to single string rows.

```js
interface Name {
    first: string;
    last: string;
}

const model = new ArrayModel<Name>([
    { first: "Hans", last: "Emil" },
    { first: "Max", last: "Mustermann" },
    { first: "Roman", last: "Tisch" },
]);

const mappedModel = new MapModel(model, (data) => data.last + ", " + data.first);

// prints "Emil, Hans"
console.log(mappedModel.rowData(0));
```

## Constructors

### Constructor

> **new MapModel**\<`T`, `U`\>(`sourceModel`, `mapFunction`): `MapModel`\<`T`, `U`\>

Defined in: [api/node/typescript/models.ts:566](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L566)

Constructs a new MapModel that provides a mapped view on the given
`sourceModel` by applying `mapFunction` on each of its rows.

#### Parameters

##### sourceModel

[`Model`](/latest/docs/node/api/classes/model/)\<`T`\>

the wrapped model.

##### mapFunction

(`data`) => `U`

maps the data from T to U. It must not modify `sourceModel`.

#### Returns

`MapModel`\<`T`, `U`\>

#### Overrides

`NativeAdapterModel<U>.constructor`

## Properties

### sourceModel

> `readonly` **sourceModel**: [`Model`](/latest/docs/node/api/classes/model/)\<`T`\>

Defined in: [api/node/typescript/models.ts:558](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L558)

The source model that this MapModel maps rows from.

## Accessors

### \[nativeModelKey\]

#### Get Signature

> **get** **\[nativeModelKey\]**(): `NativeModel` \| `undefined`

Defined in: [api/node/typescript/models.ts:424](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L424)

##### Returns

`NativeModel` \| `undefined`

#### Inherited from

`NativeAdapterModel.[nativeModelKey]`

## Methods

### \[iterator\]()

> **\[iterator\]**(): `Iterator`\<`U`\>

Defined in: [api/node/typescript/models.ts:195](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L195)

#### Returns

`Iterator`\<`U`\>

#### Inherited from

`NativeAdapterModel.[iterator]`

***

### filter()

> **filter**(`filterFunction`): [`FilterModel`](/latest/docs/node/api/classes/filtermodel/)\<`U`\>

Defined in: [api/node/typescript/models.ts:118](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L118)

Returns a new Model that only contains the rows of this model for which
`filterFunction` returns true.

#### Parameters

##### filterFunction

(`data`) => `boolean`

returns true if a row should be visible.

#### Returns

[`FilterModel`](/latest/docs/node/api/classes/filtermodel/)\<`U`\>

a new [FilterModel](/latest/docs/node/api/classes/filtermodel/) that wraps the current model.

#### Inherited from

`NativeAdapterModel.filter`

***

### insertRow()

> **insertRow**(`_index`, `_data`): `void`

Defined in: [api/node/typescript/models.ts:191](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L191)

Implementations of this function must add a row at the specified index, pushing all next
rows to the right.
Throws when the model rejects the modification; the default implementation
always throws.

#### Parameters

##### \_index

`number`

index of the row to insert.

##### \_data

`U`

new data item to store in a new row.

#### Returns

`void`

#### Inherited from

`NativeAdapterModel.insertRow`

***

### map()

> **map**\<`U`\>(`mapFunction`): `MapModel`\<`U`, `U`\>

Defined in: [api/node/typescript/models.ts:108](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L108)

Returns a new Model where all elements are mapped by the function `mapFunction`.

#### Type Parameters

##### U

`U`

#### Parameters

##### mapFunction

(`data`) => `U`

maps the data from T to U.

#### Returns

`MapModel`\<`U`, `U`\>

a new [MapModel](/latest/docs/node/api/classes/mapmodel/) that wraps the current model.

#### Inherited from

`NativeAdapterModel.map`

***

### pushRow()

> **pushRow**(`data`): `void`

Defined in: [api/node/typescript/models.ts:169](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L169)

Adds a line to the model with the provided data.
Throws when the model rejects the modification.
The default implementation calls [Model.insertRow](/latest/docs/node/api/classes/model/#insertrow) with the row count.

#### Parameters

##### data

`U`

new data item to store in a new row.

#### Returns

`void`

#### Inherited from

`NativeAdapterModel.pushRow`

***

### removeRow()

> **removeRow**(`_index`): `void`

Defined in: [api/node/typescript/models.ts:179](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L179)

Implementations of this function must remove the row at the specified index.
Throws when the model rejects the modification; the default implementation
always throws.

#### Parameters

##### \_index

`number`

index of the row to remove.

#### Returns

`void`

#### Inherited from

`NativeAdapterModel.removeRow`

***

### reverse()

> **reverse**(): [`ReverseModel`](/latest/docs/node/api/classes/reversemodel/)\<`U`\>

Defined in: [api/node/typescript/models.ts:137](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L137)

Returns a new Model with the same rows as this model, in reverse order.

#### Returns

[`ReverseModel`](/latest/docs/node/api/classes/reversemodel/)\<`U`\>

a new [ReverseModel](/latest/docs/node/api/classes/reversemodel/) that wraps the current model.

#### Inherited from

`NativeAdapterModel.reverse`

***

### rowCount()

> **rowCount**(): `number`

Defined in: [api/node/typescript/models.ts:436](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L436)

Returns the number of entries in the model.

#### Returns

`number`

#### Inherited from

`NativeAdapterModel.rowCount`

***

### rowData()

> **rowData**(`row`): `U` \| `undefined`

Defined in: [api/node/typescript/models.ts:445](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L445)

Returns the data at the specified row.

#### Parameters

##### row

`number`

index in range 0..(rowCount() - 1).

#### Returns

`U` \| `undefined`

undefined if row is out of range otherwise the data.

#### Inherited from

`NativeAdapterModel.rowData`

***

### setRowData()

> **setRowData**(`_row`, `_data`): `void`

Defined in: [api/node/typescript/models.ts:583](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L583)

Throws, because a MapModel can't map a `U` back to the `T` that its source model stores.

#### Parameters

##### \_row

`number`

##### \_data

`U`

#### Returns

`void`

#### Overrides

`NativeAdapterModel.setRowData`

***

### sort()

> **sort**(`compareFunction`): [`SortModel`](/latest/docs/node/api/classes/sortmodel/)\<`U`\>

Defined in: [api/node/typescript/models.ts:129](https://github.com/slint-ui/slint/blob/master/api/node/typescript/models.ts#L129)

Returns a new Model with the same rows as this model, ordered according to
`compareFunction`.

#### Parameters

##### compareFunction

(`a`, `b`) => `number`

compares two rows the same way the callback passed to
                        Array.prototype.sort does.

#### Returns

[`SortModel`](/latest/docs/node/api/classes/sortmodel/)\<`U`\>

a new [SortModel](/latest/docs/node/api/classes/sortmodel/) that wraps the current model.

#### Inherited from

`NativeAdapterModel.sort`