1
var text = '{ "nusers" : [' +
'{ "firstName":"John" , "lastName":"Doe" },' +
'{ "firstName":"Anna" , "lastName":"Smith" },' +
'{ "firstName":"sadsf" , "lastName":"sdasda" },' +
'{ "firstName":"fnSaleh" , "lastName":"lnSaleh" }]}';

var va = "nuser";
 obj = JSON.parse(text);

for(i=0;i< (obj.va.length);i++){

var st = obj.va.firstName;

console.log(st);
}

Error

"Uncaught TypeError: Cannot read property 'length' of undefined"

4 Answers4

3

You have several problems.

For starters use [] notation for variable property names. Then fix value of va to match the object property nusers not nuser. Finally, use index i to access the array elements within the loop

var text = '{ "nusers" : [' +
  '{ "firstName":"John" , "lastName":"Doe" },' +
  '{ "firstName":"Anna" , "lastName":"Smith" },' +
  '{ "firstName":"sadsf" , "lastName":"sdasda" },' +
  '{ "firstName":"fnSaleh" , "lastName":"lnSaleh" }]}';

var va = "nusers";
obj = JSON.parse(text);

for (i = 0; i < (obj[va].length); i++) {

  var st = obj[va][i].firstName;

  console.log(st);
}
charlietfl
  • 169,044
  • 13
  • 113
  • 146
1

Use square bracket when accessing an object property using a variable. Also you need to get the index so obj[va][i].firstName will give the first name

var text = '{ "nusers" : [' +
  '{ "firstName":"John" , "lastName":"Doe" },' +
  '{ "firstName":"Anna" , "lastName":"Smith" },' +
  '{ "firstName":"sadsf" , "lastName":"sdasda" },' +
  '{ "firstName":"fnSaleh" , "lastName":"lnSaleh" }]}';

var va = "nusers";
var obj = JSON.parse(text);
for (var i = 0; i < (obj[va].length); i++) {
  var st = obj[va][i].firstName;
  console.log(st);
}
brk
  • 46,805
  • 5
  • 49
  • 71
1

First of all, declare all variabes on top.

Then use the right key ('nusers' with s at the end) for accessing the object.

For accessing an object, you need a property accessor which is here with bracket notation, because you have a variable. This is also necessary for accessing an array with an index.

var text = '{ "nusers" : [' +
    '{ "firstName":"John" , "lastName":"Doe" },' +
    '{ "firstName":"Anna" , "lastName":"Smith" },' +
    '{ "firstName":"sadsf" , "lastName":"sdasda" },' +
    '{ "firstName":"fnSaleh" , "lastName":"lnSaleh" }]}',
    va = "nusers",
    obj = JSON.parse(text),
    i,
    st;

for (i = 0; i < obj[va].length; i++) {
    st = obj[va][i].firstName;
    console.log(st);
}
Nina Scholz
  • 351,820
  • 24
  • 303
  • 358
1

Basically there are several problems in your code.

  1. va = 'nuser' --> This need to be nusers
  2. You should use [] notation for accessing property whenever you're using a variable to access the property.
  3. In the loop you need to use index too to access the first name.

var text = '{ "nusers" : [' +'{ "firstName":"John" , "lastName":"Doe" },' +'{ "firstName":"Anna" , "lastName":"Smith" },' +'{ "firstName":"sadsf" , "lastName":"sdasda" },' +'{ "firstName":"fnSaleh" , "lastName":"lnSaleh" }]}';

var va = "nusers";  
var obj = JSON.parse(text);

for(i=0; i< obj[va].length; i++){
  var st = obj[va][i].firstName;
  console.log(st);
}
Code Maniac
  • 35,187
  • 4
  • 31
  • 54