0

I have this json

var jsonobj = {'fieldname1':'test1','fieldname2':'test2'};

and I want to make the same process in Javascript or jQuery for say both jsonobj element...

I have tried

var obj = JSON.parse(jsonobj);
var fieldList = ['fieldname1','fieldname2'];
$.each( fieldList , function( i, field ) {
alert(obj.field);
}); 

waiting for an answer like alert('test1') then alert('test2') but not working... any idea if this is possible in a way or in another? I have also tried with obj.{field}, obj.[field] but no way, doesn't work! ^^'

Thanks much for your help!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Snipchain
  • 147
  • 2
  • 12

1 Answers1

1

Use brackets to get the named property.

var obj = JSON.parse(jsonobj);
var fieldList = ['fieldname1','fieldname2'];
$.each( fieldList , function( i, field ) {
    alert(obj[field]);
}); 
Justice Erolin
  • 2,869
  • 20
  • 19
  • pfffff... thanks much! Just realized the "." problem... sometimes easy stuff is the hardest to realize.. ^^' – Snipchain Dec 03 '19 at 19:37