Eval Makes Length Undefined

JavaScript related questions should go here.
Post Reply
unemployment
Posts: 165
Joined: Fri May 06, 2011 5:02 pm

Eval Makes Length Undefined

Post by unemployment »

I have a json encoded array

[syntax=text]{"users":[{"row_id":"1","0":"Tom","1":"Cruise","2":"tomcruise","3":"1","4":"231","5":"10","6":"Old","7":"235","8":"532","9":"12"},{"row_id":"21","0":"mrs. tom","1":"cruise","2":"place","3":"0","4":null,"5":null,"6":null,"7":"6","8":"42","9":"0"}]}[/syntax]

My javascript is...

[syntax=javascript]
var search = eval('(' + resp + ')');
var search_wrap =[];

alert(search.length);
[/syntax]

When my function returns the json encoded array, my alert is undefined. Is this because my array is multidimensional?

[syntax=php]Array
(
[users] => Array
(
[0] => Array
(
[row_id] => 1
[0] => Tom
[1] => Cruise
[2] => tomcruise
)

[1] => Array
(
[row_id] => 21
[0] => mrs tom
[1] => cruise
[2] => mrstomcruise
)

)

)[/syntax]
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Eval Makes Length Undefined

Post by jacek »

unemployment wrote: Is this because my array is multidimensional?

No it's because objects don't have a length property ;)

You can do

[syntax=javascript]search.users.length;[/syntax]
Image
unemployment
Posts: 165
Joined: Fri May 06, 2011 5:02 pm

Re: Eval Makes Length Undefined

Post by unemployment »

jacek wrote:
unemployment wrote: Is this because my array is multidimensional?

No it's because objects don't have a length property ;)

You can do

[syntax=javascript]search.users.length;[/syntax]


Yes, you're right. You can basically do what you suggested.
Post Reply