angular2-forms - 以角度 2 更新对象的值
问题描述
我必须在特定索引处更新我的对象的值名称属性。我有索引号和数组。这是我的代码,
this.menus.forEach(x => {
let temp = this.menus.findIndex(function (item) {
return item.name == 'Cost Center';
});
// this.menus.values[temp].name=this.newCC;
this.menus[temp].name=this.newCC;
console.log("menu temp",temp);
这是我的数组结构,Array(11)
0:{_id: "5acc9e6320a7d71ab8372120", name: "DashBoard", link: "dashboard", icon: "fa fa-list", key: "dashboard", …}
1: {_id: "5acc9e6320a7d71ab837211f", name: "My Products", link: "print-products", icon: "fa fa-barcode", key: "print-products", …}
2:{sequence: null, key: "my-saved-jobs", icon: "fa fa-file-text-o", link: "my-saved-jobs", name: "My Saved Jobs", …}
3:{sequence: null, key: "my-placed-orders", icon: "fa fa-list-alt", link: "my-placed-orders", name: "My Orders", …}
4:{_id: "5acc9e6320a7d71ab837211b", name: "Site Design Setup", link: "storefront-setup", icon: "fa fa-university", key: "storefront-setup", …}
5:{_id: "5acc9e6320a7d71ab8372118", name: "Reports", link: "reports", icon: "icon-report", key: "reports", …}
6:{_id: "5accb9425241ed3270585b93", name: "Print Service Category", link: "print-service-category", icon: "fa fa-tasks", key: "print-service-category", …}
7:{sequence: null, key: "my-saved-files", icon: "fa fa-floppy-o", link: "my-saved-files", name: "My Saved Files", …}
8:{sequence: null, key: "product-list", icon: "fa fa-list", link: "product-list", name: "Product List", …}
9:{_id: "5acc9e6320a7d71ab8372119", name: "Deal Code", link: "deal- code", icon: "fa fa-university", key: "deal-code", …}
10:{sequence: null, key: "cost-center", icon: "icon-costCenter", link: "cost-center", name: undefined, …}
length:11
任何人都可以帮助解决我的问题吗?请回复。
解决方案
实际上我在编写代码时犯了一个错误,这段代码应该写在我们从后端获得响应的地方,即在成功响应的范围内。这是我的新代码,
this.httpService.save(UrlDetails.$getPaymentSetting, client)
.subscribe(response => {
if (response.responseCode == 200) {
let paymentSetting = response.responseData;
console.log("client PAYMENT RESPONSE ",paymentSetting);
if( paymentSetting.paymentOption.customPayment.length != 0){
<------Some Code----->
.
.
this.menus.forEach(x => {
let temp1 = this.menus.findIndex(function (item) {
return item.name == 'Cost Center';
});
let temp2 = this.menus.findIndex(function (item) {
return item.name == 'Deal Code';
});
this.menus[temp1].name= this.newCC;
this.menus[temp2].name= this.newDC;
});
}
})