检查数组的空值或未定义值?
check null or undefined value for array?
我有一个以下格式的数组,我必须检查是否有任何空值或未定义值
[{
"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'})
});
}
使用callback
到JSON.stringify
作为穷人遍历对象的方式:
function hasNull(obj) {
let result = false;
JSON.stringify(obj, (_, v) => v === null || v === undefined ? result = true : v);
return result;
}
我有一个以下格式的数组,我必须检查是否有任何空值或未定义值
[{
"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'})
});
}
使用callback
到JSON.stringify
作为穷人遍历对象的方式:
function hasNull(obj) {
let result = false;
JSON.stringify(obj, (_, v) => v === null || v === undefined ? result = true : v);
return result;
}