最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Find if any item in the array matches the condition - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question edited Oct 3, 2018 at 11:51 Ms.Tamil 3501 silver badge14 bronze badges asked Oct 3, 2018 at 10:29 ganesh kaspateganesh kaspate 2,68512 gold badges49 silver badges103 bronze badges 1
  • You can use Array's some method here. some() returns true (whereas find() returns the found value) when the condition matches. – Ms.Tamil Commented Oct 3, 2018 at 10:36
Add a comment  | 

6 Answers 6

Reset to default 8

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 === '';
  });
}

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;

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

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

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;

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

发布评论

评论列表(0)

  1. 暂无评论