javascript - ne mogu pristupiti dinamički kreiranom svojstvu objekta u polju u angularjs2+


Imam niz stavki kao :-

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

jedan niz zaglavlja je :-

this.headers = [
  { name: 'id', show: true },
  { name: 'name', show: true },
  { name: 'address', show: false }
];

Radim nešto ovako :-

let obj = this.headers[0].name; // this is coming as 'id'
console.log(this.itemList[0].id) // this showing as 1 

ovo je točno, ali kada radim :-

console.log(this.itemList[0].obj) // it says undefined, why?

Postoji li neki način da dobijem tu vrijednost? Hvala

Odgovor #1

Trebao bi to raditi ovako:

this.itemList[0][obj]

Razlog :

Zašto ovo ne radi?

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

Ali ovo :

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