无法访问 angularjs 2 中动态创建的数组对象 属性
cant access dynamically created obj propery of an array in anglarjs2+
我有一个像这样的项目数组:-
this.itemList = [
{ id: 1, name: 'a', address: 'as dasf a' },
{ id: 2, name: 'b', address: 'as dasf a' },
{ id: 3, name: 'c', address: 'as dasf a' }
];
一个headers数组是:-
this.headers = [
{ name: 'id', show: true },
{ name: 'name', show: true },
{ name: 'address', show: false }
];
我正在做类似的事情:-
let obj = this.headers[0].name; // this is coming as 'id'
console.log(this.itemList[0].id) // this showing as 1
这是正确的,但是当我这样做的时候:-
console.log(this.itemList[0].obj) // it says undefined, why?
有什么办法可以获得那个值吗?
谢谢
你应该这样做:
this.itemList[0][obj]
原因:
为什么这不起作用?
this.itemList[0].obj
// this will not work coz it will try to find 'obj' as key in itemList[0]
// so it will return undefined
但是这个:
this.itemList[0][obj]
// But in this one obj considered as varible and replace with its value
// so it will become this.itemList[0].id / this.itemList[0]['id']
我有一个像这样的项目数组:-
this.itemList = [
{ id: 1, name: 'a', address: 'as dasf a' },
{ id: 2, name: 'b', address: 'as dasf a' },
{ id: 3, name: 'c', address: 'as dasf a' }
];
一个headers数组是:-
this.headers = [
{ name: 'id', show: true },
{ name: 'name', show: true },
{ name: 'address', show: false }
];
我正在做类似的事情:-
let obj = this.headers[0].name; // this is coming as 'id'
console.log(this.itemList[0].id) // this showing as 1
这是正确的,但是当我这样做的时候:-
console.log(this.itemList[0].obj) // it says undefined, why?
有什么办法可以获得那个值吗? 谢谢
你应该这样做:
this.itemList[0][obj]
原因:
为什么这不起作用?
this.itemList[0].obj
// this will not work coz it will try to find 'obj' as key in itemList[0]
// so it will return undefined
但是这个:
this.itemList[0][obj]
// But in this one obj considered as varible and replace with its value
// so it will become this.itemList[0].id / this.itemList[0]['id']