如何从关键中获取价值

时间:2019-06-26 14:31:04

标签: json angular web-services

push

我在数组中获得键A,B

在角度2中如何根据提供的键(A或B)获取值...这里的值是一个对象 如何将其解析为角度为2的模型

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

        }
}

这是我的json对象

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
    ) { }

2 个答案:

答案 0 :(得分:0)

这与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的A属性分配给obj2的A属性。

答案 1 :(得分:0)

但是您可以使用angular forEach来做到这一点

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