简体   繁体   English

如何从关键中获取价值

[英]How get values from key

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

        }
}

i am getting keys A,B in an array 我在数组中获取键A,B

In angular 2 how to get values based on provided key (A or B)... here value is an objects how to parse it into an model in angular 2 在角度2中,如何基于提供的键(A或B)获取值...这里,值是一个对象,如何将其解析为角度2中的模型

for(let key of this.keys){
        if(key == "A"){
            // how to get value and assign that value to object}}`

here is my json object 这是我的json对象

export class Installment {
    constructor(
        public isResurring: boolean,
        public isInstallment: boolean,
        public price: string,
        public sku: string
    ) { }

//Json Object // Json对象

{"A":{"isInstallment":true,"isRecurring":true,"price":"4.0","sku":"abc"},
"B":{"isInstallment":false,"isRecurring":true,"price":"1.0","sku":"def"}}

This is not related to Angular, this is related to Javascript. 这与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"}

This code will assign the A property from obj1 into A property of obj2. 此代码会将obj1的A属性分配给obj2的A属性。

but you can use angular forEach to do that 但您可以使用angular forEach做到这一点

var k = 'A'
angular.forEach(data, function(value, key){
 if (k == key) {
   doSomething()
 }
})

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

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