我有一个以下格式的数组,我必须检查是否有任何null或未定义的值
[{
"categoryName": "A",
"product": [{
"catogoryName": "A",
"price": "10",
"productName": "a"
}, {
"catogoryName": "A",
"price": "100",
"productName": "a2"
}, {
"catogoryName": "A",
"price": "5654",
"productName": "dfhdh"
}, {
"catogoryName": "A",
"price": "572",
"productName": "d"
}]
}, {
"categoryName": "B",
"product": [{
"catogoryName": "B",
"price": "10",
"productName": "b"
}, {
"catogoryName": "B",
"price": "300",
"productName": "b2"
}]
}]
我尝试使用以下代码,但它总是给我success:1
消息。
var validateCategoryList = function (categoryList,callback) {
async.eachSeries(categoryList, function (categoryChunk, callback) {
var categoryName = categoryChunk.categoryName;
var productList = categoryChunk.product;
console.log(categoryName);
if(!(categoryName == null || categoryName == undefined)){
async.eachSeries(productList, function (item, callback) {
var productName = item.productName;
var price = item.price;
console.log(productName);
console.log(price);
if((!(productName == null || price == undefined)) || (!(productName == null || price == undefined))){
callback(null);
}
else{
callback({'success':'0','result':{},'errorMessage':'CategoryLIst invalid'})
return;
}
},function (err) {
callback(null);
});
}
else{
callback({'success':'0','result':{},'errorMessage':'CategoryLIst invalid'})
return;
}
},function (err) {
callback({'success':'1'})
});
}
答案 0 :(得分:2)
使用addLegend()
callback
作为穷人的方式来遍历对象:
JSON.stringify