React 原生嵌套对象循环与 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].我认为承诺会解决问题,但我不知道我将如何在这种嵌套情况下使用它们。

由于您想使用带有上述问题的承诺,我用承诺更新了您的代码,看看。

    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:"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 中,输出应该是 [0,3,5]3 个空字段用于student_b电子邮件和电话号码无效。它把这个条件放在哪里?

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:"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]

现场演示

相关内容

  • 没有找到相关文章

最新更新