angular - 使用 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]”。
解决方案
Lodashpick
描述为:
创建由拾取的对象属性组成的对象。
你this.animals
是一个数组。这可能是您正在寻找的:
this.animals.map(animal => pick(animal, keys(['name,age,gender'])));
推荐阅读
- python - 为每个重复的 N 行创建一行
- c# - cshtml onclick 事件中的按钮从 cshtml.cs 读取函数
- angular - 如何在 Angular 6 中禁用 simplemde?
- c# - 来自 NPC 的光线投射
- node.js - AWS Lambda/Chromium - 评估期间定义的函数“未定义”
- javascript - Cloud Functions .onCall() 无法返回数据
- oracle - Oracle 用另一个表的主键值插入和替换值
- python - 使用“+”运算符是否会转换为 Python 中列表的更高数据类型?
- javascript - 解决功能组件中的“对象作为 React 子项无效(找到:[object Promise])”
- symfony - 呈现表单时忽略我的自定义表单类型模板