Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法访问anglarjs2+;_Javascript_Angular_Typescript_Typescript2.0 - Fatal编程技术网

Javascript 无法访问anglarjs2+;

Javascript 无法访问anglarjs2+;,javascript,angular,typescript,typescript2.0,Javascript,Angular,Typescript,Typescript2.0,我有一个项目数组,如:- 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' } ]; 一个标题数组是:- this.headers = [ { name: 'id', show: true }, { name: 'name', sho

我有一个项目数组,如:-

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' }
];
一个标题数组是:-

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']

你应该解释为什么你可以写这个。这将有助于OP(和其他用户)理解。@ADreNaLiNe DJ,我添加了解释,但首先发布了答案:),谢谢BTW谢谢,伙计,你能解释一下为什么它没有像我那样工作吗doing@AlokKamboj,我已经在回答中解释过了,请再看一下评论,希望这能帮你清理干净doubts@AlokKamboj,请你接受并投票表决好吗?