Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
97
rated 0 times [  100] [ 3]  / answers: 1 / hits: 85139  / 12 Years ago, thu, december 20, 2012, 12:00:00

I have an object like this:



ricHistory = {
name1: [{
test1: value1,
test2: value2,
test3: value3
}],
name2: [{
test1: value1,
test2: value2,
test3: value3
}]
};


Now I want to check if e.g. name2 is empty with Javascript/jQuery. I know the method hasOwnProperty. It work for data.hasOwnProperty('name2') only if the name exists or not, but I have to check if its empty.


More From » jquery

 Answers
17

Try this:



if (ricHistory.name2 && 
ricHistory.name2 instanceof Array &&
!ricHistory.name2.length) {
console.log('name2 is empty array');
} else {
console.log('name2 does not exists or is not an empty array.');
}


The solution above will show you whether richHistory.name2 exists, is an array and it's not empty.


[#81304] Wednesday, December 19, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jennie

Total Points: 593
Total Questions: 102
Total Answers: 106

Location: Federated States of Micronesia
Member since Fri, Sep 16, 2022
2 Years ago
jennie questions
;