用promise原生嵌套对象循环

时间:2019-01-11 07:01:37

标签: javascript reactjs react-native es6-promise

我有一个JSON对象,如下所示

String Query = File.ReadAllText(System.Web.HttpContext.Current.Server.MapPath(ConfigurationData.QueryPath + "\\BiBordxSummery.sql"));
Query = Query.Replace("{bordexId}",bordx.Id.ToString());
var result = session.CreateSQLQuery(Query);
result.ExecuteUpdate();

在上面的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:""
        }
    }

但是我得到的输出是_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]。 我认为Promise可以解决问题,但是我不知道在这种嵌套情况下如何使用它们。

6 个答案:

答案 0 :(得分:1)

由于您想将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) });
    }

答案 1 :(得分:0)

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)

答案 2 :(得分:0)

完美的以下代码可以正常工作

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] 电子邮件和电话号码无效,student_b的3个空白字段。 这种情况放在哪里?

答案 3 :(得分:0)

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);

答案 4 :(得分:0)

这将正常工作。

 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)

答案 5 :(得分:0)

对于这种格式

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]

live demo