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

2024-05-08

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

"revenues":[
{
        "drug_id":"20",
        "quantity":10
},
{
        "drug_id":"30",
        "quantity":1    
}]

所以我想从所有对象中删除 drug_id 。 我怎样才能做到这一点? 谢谢你!


你可以用它:

this.revenues = this.revenues.map(r => ({quantity: r.quantity}));

对于更通用的方法:

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;
  });
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从打字稿中的数组中删除对象 的相关文章

随机推荐