首页 > 解决方案 > 使用 Lodash 从列表中删除项目

问题描述

我有一个列表,其中模型如下:

动物模型

    id
    name
    age
    gender
    city

animals[] = [];我拥有的列表中,我需要删除字段name, age and gender并保留id, and city在其中。我怎样才能做到这一点 ?

我尝试了什么:

    import { pick,keys } from 'lodash';

    this.animals = pick(this.animals, keys(['name,age,gender']));

我得到的错误

[ts] 类型“PartialDeep”不可分配给类型“Animal[]”。“PartialDeep”类型中缺少属性“[Symbol.unscopables]”。

标签: angulartypescriptlodash

解决方案


Lodashpick描述为:

创建由拾取的对象属性组成的对象。

this.animals是一个数组。这可能是您正在寻找的:

this.animals.map(animal => pick(animal, keys(['name,age,gender'])));

推荐阅读