在typescript中从数组中删除对象

Bèç*_*íkè 3 typescript angular2-template angular

如何从打字稿中的数组中删除对象?

"revenues":[
{
        "drug_id":"20",
        "quantity":10
},
{
        "drug_id":"30",
        "quantity":1    
}]
Run Code Online (Sandbox Code Playgroud)

所以我想从所有对象中删除drug_id.我怎么做到这一点?谢谢!

小智 5

你可以用它:

this.revenues = this.revenues.map(r => ({quantity: r.quantity}));
Run Code Online (Sandbox Code Playgroud)

有关更通用的方法:

removePropertiesFromRevenues(...props: string[]) {
  this.revenues = this.revenues.map(r => {
    const obj = {};
    for (let prop in r) { if (!props.includes(prop) { obj[prop] = r[prop]; } }
    return obj;
  });
}
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

6627 次

最近记录:

7 年,10 月 前