CxJS

append

import { append } from 'cx/data'; Copied

append creates a new array with items added to the end. Handles null/undefined arrays gracefully.

<div controller={PageController}>
  <Grid
    records={m.items}
    columns={[
      { header: "ID", field: "id", align: "center" },
      { header: "Name", field: "name" },
    ]}
  />
  <div style="margin-top: 16px; display: flex; gap: 8px; align-items: center; flex-wrap: wrap">
    <TextField value={m.newName} placeholder="Enter name..." />
    <Button onClick="addItem">Add Item</Button>
    <Button onClick="addMultiple">Add Multiple</Button>
    <Button onClick="reset">Reset</Button>
  </div>
</div>
IDName
1First
2Second

Signature

function append<T>(array: T[], ...items: T[]): T[]

Parameters

ParameterTypeDescription
arrayT[]The source array (can be null/undefined).
itemsT[]Items to append.

Return Value

Returns a new array with items appended. If array is null/undefined, returns the items as a new array.

Examples

Basic usage

const items = [1, 2, 3];
const result = append(items, 4, 5);
// [1, 2, 3, 4, 5]

// Original array is unchanged
console.log(items); // [1, 2, 3]

With null array

const result = append(null, 1, 2);
// [1, 2]

No items to append

const items = [1, 2, 3];
const result = append(items);
// [1, 2, 3] - returns empty array if source is null, otherwise same reference

See Also