export class Installment {
constructor(
public isResurring: boolean,
public isInstallment: boolean,
public price: string,
public sku: string
) { }
this.keys = Object.keys(this.paymentPlans);
for(let key of this.keys){
if(key == "A"){
// how to get value and assign that value to above object
}
}
我在数组中获得键A,b
在Angular 2中如何基于提供的键(A或B(获得值...这里值是对象如何将其解析为Angular 2
中的模型for(let key of this.keys){
if(key == "A"){
// how to get value and assign that value to object}}`
这是我的JSON对象
export class Installment {
constructor(
public isResurring: boolean,
public isInstallment: boolean,
public price: string,
public sku: string
) { }
//json对象
{"A":{"isInstallment":true,"isRecurring":true,"price":"4.0","sku":"abc"},
"B":{"isInstallment":false,"isRecurring":true,"price":"1.0","sku":"def"}}
,但您可以使用Angular foreach做
var k = 'A'
angular.forEach(data, function(value, key){
if (k == key) {
doSomething()
}
})
这与Angular无关,这与JavaScript有关。
const keys = ['A'];
const obj1 = {
A: 'Some value',
B: 'Some other value'
};
const obj2 = {};
for (let key of keys)
obj2[key] = obj1[key]; // <---- This is the way to access an object property with a string
console.log(obj2); // {"A": "Some value"}
此代码将将OBJ1的属性分配给OBJ2的属性。