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
  • 170,828
  • 13
  • 121
  • 150
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
  • 48,835
  • 10
  • 56
  • 78
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
  • 376,160
  • 25
  • 347
  • 392
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
  • 37,143
  • 5
  • 39
  • 60