[英]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.