Find if any item in the array matches the condition

99封情书 提交于 2019-12-13 03:35:20

问题


I am new to Javascript. Now, Here I have an array which has multiple objects. So, I want to iterate it and if any of the object matches the condition then I want to return a value and stop that loop.

My array of obj is like,

var obj =  [ { type: "", numberOfQuestions:"",  technology:"" }, { type: "1", numberOfQuestions:"4",  technology:"abcd" }, { type: "", numberOfQuestions:"6",  technology:"ass" } ]

And my condition is,

     validateData(data) {
            data.High.map((object) => {
              if((object.type === "") || (object.numberOfQuestions === "") || (object.technology === "")) {
                    return true;
              } else {
                  return false;
              } 
            });
        } 

So, what I want is that any of the object which has some keys, has empty value for any key, i.e "" Then I want to return a true value so that I can do some other stuff. How can I do this ?

Can anyone please help me.


回答1:


You can use Array.prototype.some

var array = [...];

function validateData (array) {
  return array.some(item => item.type === '' || item.numberOfQuestions === '' || item.technology === '');
}

validateData(array);

It was ES6 solution (with arrow functions).

ES5 solution:

function validateData (array) {
  return array.some(function(item) { 
    return item.type === '' || item.numberOfQuestions === '' || item.technology === '';
  });
}



回答2:


You can use filter function for this, which return the array on the condition

 var container =  [ { type: "", numberOfQuestions:"",  technology:"" }, { type: "1", numberOfQuestions:"4",  technology:"abcd" }, { type: "", numberOfQuestions:"6",  technology:"ass" } ]

    container.filter((a)=>{ return a['type'] == "" ? a : a['numberOfQuestions'] == "" ? a : a['technology'] == "" ? a : '' }).length > 0 ? true : false;



回答3:


First off, dont use = in the object please use : . If you want to check the keys dynamically use this code

    const validateData = (data) => {
          data.map((object) => {
        Object.keys(object).map((innerObj) => {
        if(object[innerObj] === "") {
                  return true;
            } else {
                return false;
            } 

        })

          });
        } 

        var obj =  [{ type: "", numberOfQuestions:"",  technology:"" }, 
{ type: "1", numberOfQuestions:"4",  technology:"abcd" }, 
{ type: "", numberOfQuestions:"6",  technology:"ass" }];

        validateData(obj);



回答4:


This will work regardless of key names (using es6 syntax).

var data =  [ { type: "", numberOfQuestions:"",  technology:"" }, { type: "1", numberOfQuestions:"4",  technology:"abcd" }, { type: "", numberOfQuestions:"6",  technology:"ass" } ]

const checkNull = (data) => data.some(item => Object.keys(item).some(key => item[key] == ''));

console.log(checkNull(data));



回答5:


You can use filter method

var obj =  [ { type: "", numberOfQuestions:"",  technology:"" }, { type: "1", numberOfQuestions:"4",  technology:"abcd" }, { type: "", numberOfQuestions:"6",  technology:"ass" } ]
obj.filter((a)=>{ return a['type'] == "" ? a : a['numberOfQuestions'] == "" ? a : a['technology'] == "" ? a : '' }).length > 0 ? true : false;



回答6:


use array reduce

    obj =  [ { type: "", numberOfQuestions:"",  technology:"" }, { type: "1", numberOfQuestions:"4",  technology:"abcd" }, { type: "", numberOfQuestions:"6",  technology:"ass" } ];
    
    obj = obj.find(function(item){ if (!(item.type === '' || item.numberOfQuestions === '' || item.technology === '')){ return item; } });
    
    console.log('array : ', obj);


来源:https://stackoverflow.com/questions/52625038/find-if-any-item-in-the-array-matches-the-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!