ts对象数组常用方法
    好的,以下是一些 TypeScript 对象数组常用的方法:
    1. 数组添加元素:
    ```typescript
    const arr = [1, 2, 3];
    arr.push(4);
    arr.pop();
    arr.shift();
    arr.unshift(5);
    ```
    2. 数组替换元素:
    ```typescript
    const arr = [1, 2, 3];
    arr[0] = 4;
    arr.splice(1, 1, 6);
    ```
    3. 数组删除元素:
    ```typescript
    const arr = [1, 2, 3];
    arr.splice(0, 1);
    arr.pop();
    arr.shift();
    arr.unshift(4);
    ```
    4. 数组合并元素:
    ```typescript
    const arr1 = [1, 2, 3];
    const arr2 = [4, 5, 6];
    const arr = at(arr2);
    ```
    5. 数组排序元素:
    ```typescript
    const arr = [1, 2, 3];
    arr.sort();
    ```
    6. 数组查元素:
    ```typescript
    const arr = [1, 2, 3];
    const target = 2;
    const index = arr.indexOf(target);
    ```
    7. 数组遍历元素:
    ```typescript
    const arr = [1, 2, 3];
    for (let i = 0; i < arr.length; i++) {
    const element = arr[i];
    console.log(element);
    }
    ```
    8. 对象数组常用方法:
    - 对象数组添加元素:
    ```typescript
    const objArr = [{ id: 1, na "John" }];
    const newItem = { id: 2, na "Jane" };
    objArr.push(newItem);
    ```
    - 对象数组替换元素:
    ```typescript
    const objArr = [{ id: 1, na "John" }];
    const newItem = { id: 2, na "Jane" };
    objArr.splice(0, 1, newItem);
    ```
    - 对象数组删除元素:
    ```typescript
    const objArr = [{ id: 1, na "John" }];
    const deleteItem = { id: 2, na "Jane" };
    ve(deleteItem);
    ```
    - 对象数组合并元素:
    ```typescript
    const objArr = [{ id: 1, na "John" }];
    const otherItem = { id: 2, na "Jane" };
    const newItem = { ...objArr[0], ...otherItem };
    const newObjArr = at(otherItem, newItem);
    ```
    - 对象数组排序元素:
    ```typescript
    const objArr = [{ id: 1, na "John" }];
    const otherItem = { id: 2, na "Jane" };
    objArr.sort((a, b) => a.id - b.id);
    ```
    - 对象数组查元素:
    ```typescript
    const objArr = [{ id: 1, na "John" }];
    const targetItem = { id: 2, na "Jane" };
ts 数组字符串转数组    const index = objArr.indexOf(targetItem);
    ```
    以上是 TypeScript 对象数组常用的一些方法,可以根据具体需求进行调用。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。