If I have data in json like this :
{"items":[{"id":"2049","channel_code":"HBD","channel_name":"HBO HD"}]}
And if you search my data to the server could not find results like this :
{"items":[]}
Of output as above, how do I read that my data does not exist or is empty?
I have written some code that I got but have not found the results I want.
This code :
var data = { Name: "John Doe", Age: 25, Address: null, CityState: "Denver, CO" };
for (member in data) {
if (data[member] != null)
//Do something
}
or
if (myObject == '') {
alert('this object is empty');
}
Maybe someone can help me find a way out of this example. Please help
If I have data in json like this :
{"items":[{"id":"2049","channel_code":"HBD","channel_name":"HBO HD"}]}
And if you search my data to the server could not find results like this :
{"items":[]}
Of output as above, how do I read that my data does not exist or is empty?
I have written some code that I got but have not found the results I want.
This code :
var data = { Name: "John Doe", Age: 25, Address: null, CityState: "Denver, CO" };
for (member in data) {
if (data[member] != null)
//Do something
}
or
if (myObject == '') {
alert('this object is empty');
}
Maybe someone can help me find a way out of this example. Please help
Share Improve this question asked Oct 17, 2012 at 10:45 Bertho JorisBertho Joris 1,6015 gold badges28 silver badges60 bronze badges3 Answers
Reset to default 2To check whether your array is empty, just use the respective length
property:
if ( data['items'].length < 1 ) {
// it's empty
}
You want to check if data.items.length > 0
. Assuming
var data = {"items":[]};
for (member in data) {
if (data[member] != null)
//Do something
}
code inside for
will not run because length
of data is 0
if (myObject == '') {
alert('this object is empty');
}
myObject wont be null because the object actually is there and its an empty array
you should check for myObject.length
because its an empty array