简体   繁体   English

如何删除 object 属性并分配新变量?

[英]How to delete object property and assign in new variable?

I've issue when take object and use.map() to delete a property from it and assign it to a new variable, actually it's working but also is removing the property from the obj products which I don't want.我在使用 object 和 use.map() 从中删除一个属性并将其分配给一个新变量时遇到问题,实际上它正在工作,但也正在从我不想要的 obj products中删除该属性。

filteredProduct;

calcShipPrice() {
  this.checkoutForm.patchValue({
  product: this.products,
  delivery: this.selectedDelivery,
  total: this.totalPrice
});
// remove two properties from the obj and submit just name and price of product
this.filteredProduct = this.products.map(res => delete res.image && delete res.description);
console.log(this.products);
}

Thank you!谢谢!

Try this -尝试这个 -

this.filteredProduct = this.products.map((product) => { return {
name: res.name, price: res.price
}});
console.log('this.filteredProduct', this.filteredProduct);

在此处输入图像描述

Two ways by using destructuring, is you have lots of properties this.filteredProduct = this.products.map(({image, description,... rest}) => rest);使用解构的两种方法,你有很多属性 this.filteredProduct = this.products.map(({image, description,... rest}) => rest);

Or if is just a few properties this.filteredProduct = this.products.map(({name, price}) => ({name, price}));或者如果只是几个属性 this.filteredProduct = this.products.map(({name, price}) => ({name, price}));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM