0

I have 2 objects like this

var row = {
    destination: {
        id: 1,
        name: 'test'
    },
    name: 'test2',
    source: 'source2'
};
var obj = [{'index': 'name'}, {'index': 'source'}, {'index': 'destination.name'}];

Now looping on obj I can get values of row but not for the destination.name

for(var i=0;i<obj.length;i++){
    console.log(row[obj.index]);
}

Output

test2
source2
undefined
coure2011
  • 40,286
  • 83
  • 216
  • 349

1 Answers1

0

Solution for your question

var row = {
  destination: {
    id: 1,
    name: 'test'
  },
  name: 'test2',
  source: 'source2'
};
var obj = [
 {'index': 'name'}, 
 {'index': 'source'}, 
 {'index': {'destination':'name'}}
];

for(var i=0;i<obj.length;i++){
  if(typeof(obj[i].index) == 'object'){
    var x = obj[i].index;
    var key = Object.keys(x)[0];
    var innerkey = obj[i].index[key];
    console.log(row[key][innerkey]);
  } else {
    console.log(row[obj[i].index]);
  }
}
Kishorevarma
  • 936
  • 6
  • 23