我有一个JSON对象如下
var allUsers = {
"student_a":{
id:1,
full_name:"ABC",
address:"xyz",
image:"image url"
},
"student_b":{
id:2,
full_name:"DEF",
address:"",
image:"image url"
},
"student_c":{
id:3,
full_name:"",
address:"",
image:""
}
}
在上面的JSON我需要弄清楚空字段究竟有多少在每个学生。
我使用下面的代码
_submitInfo(allUsers) {
var empty_fields = Object.entries(allUsers).map(([key, value]) => {
return this._validateStudent(value)
})
alert(JSON.stringify(empty_fields))
}
_validateStudent(studentInfo) {
empty = 0;
Object.entries(studentInfo).map(([key, value]) => {
if (value == "") {
empty++
}
})
return empty
}
但输出我得到的是[0,0,0]
所需的输出是[0,1,3]
。我认为,承诺会解决这个问题,但我不知道我怎么会在这种情况下,嵌套使用它们。
当你想使用的承诺与上述问题我承诺更新你的代码,只是来看看。
var allUsers = {
"student_a": {
id: 1,
full_name: "ABC",
address: "xyz",
image: "image url"
},
"student_b": {
id: 2,
full_name: "DEF",
address: "",
image: "image url"
},
"student_c": {
id: 3,
full_name: "",
address: "",
image: ""
}
}
check(allUsers);
function check() {
const promiseContainer = [];
Object.entries(allUsers).map(([key, value]) => {
promiseContainer.push(_validateStudent(value));
});
function _validateStudent(studentInfo) {
let empty = 0;
return new Promise((resolve, reject) => {
Object.entries(studentInfo).map(([key, value]) => {
if (value == "") {
empty++
}
})
resolve(empty);
});
}
Promise.all(promiseContainer).then((count) => { console.log(count) });
}
var allUsers = {
"student_a":{
id:1,
full_name:"ABC",
address:"xyz",
image:"image url"
},
"student_b":{
id:2,
full_name:"DEF",
address:"",
image:"image url"
},
"student_c":{
id:3,
full_name:"",
address:"",
image:""
}
}
var result = Object.keys(allUsers).map(student => Object.values(allUsers[student]).reduce((n, v) => {
if(!v.toString().length) n += 1;
return n
},0))
console.log(result)
这将正常工作。
var data = {
"student_a":{
id:1,
full_name:"ABC",
address:"xyz",
image:"image url"
},
"student_b":{
id:2,
full_name:"DEF",
address:"",
image:"image url"
},
"student_c":{
id:3,
full_name:"",
address:"",
image:""
}
}
let output =[]
for (let value of Object.values(data)) {
var test = Object.values(value)
var lucky = test.filter(function(number) {
return number == "";
});
output.push(lucky.length)
}
console.log(output)
完善以下代码工作
var result = Object.keys(allUsers).map(student => Object.values(allUsers[student]).reduce((n, v) => {
if(!v.toString().length) n += 1;
return n
},0))
但还有更多的问题,什么如果JSON如下
var allUsers = {
"student_a":{
id:1,
full_name:"ABC",
address:"xyz",
email:"[email protected]",
number:"1234567890",
image:"image url"
},
"student_b":{
id:2,
full_name:"DEF",
address:"",
email:"random value",
number:"000",
image:"image url"
},
"student_c":{
id:3,
full_name:"",
address:"",
email:"",
number:"",
image:""
}
}
并且有一个电子邮件地址和电话号码,如果无效,我认为它是空白。所以在上面的JSON输出应为[0,3,5]为student_b 3个空字段如电子邮件和电话号码无效。在哪里它把这种状况呢?
const allUsers= {
"student_a":{
id:1,
full_name:"ABC",
address:"xyz",
image:"image url"
},
"student_b":{
id:2,
full_name:"DEF",
address:"",
image:"image url"
},
"student_c":{
id:3,
full_name:"",
address:"",
image:""
}
};
let arr=[];
Object.entries(allUsers).map(([key,val]) => {
x= Object.values(val).reduce((count, cur) => {
if(cur == ""){
return ++count;
}
return count;
},0)
arr.push(x);
});
console.log(arr);
对于这种格式
var allUsers = {
"student_a":{
id:1,
full_name:"ABC",
address:"xyz",
email:"[email protected]",
number:"1234567890",
image:"image url"
},
"student_b":{
id:2,
full_name:"DEF",
address:"",
email:"random value",
number:"000",
image:"image url"
},
"student_c":{
id:3,
full_name:"",
address:"",
email:"",
number:"",
image:""
}
}
让你的输出[0,3,5]
做这个:
Object.keys(allUsers).map(obj => allUsers[obj]).map(item => {
item.email = /\S+@\S+\.\S+/.test(item.email) ? item.email : ""; // valid email or ""
item.number = item.number ? item.number.match(/\d/g).length ===10 ? item.number : "" : item.number; //valid number or ""
return item;
}).map(item => Object.values(item).filter(innerItem => innerItem === "").length);
这将打印[0, 3, 5]