[英]React native nested object loop with promise
我有一个 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]
。 我认为承诺会解决这个问题,但我不知道我将如何在这种嵌套情况下使用它们。
由于您想在上述问题中使用 Promise,因此我使用 Promise 更新了您的代码,请看一下。
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 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:"a@a.com",
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 中,student_b 的输出应该是 [0,3,5] 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 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 allUsers = {
"student_a":{
id:1,
full_name:"ABC",
address:"xyz",
email:"a@a.com",
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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.