ganesh kaspate
ganesh kaspate

Reputation: 2685

Find if any item in the array matches the condition

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.

Upvotes: 8

Views: 14563

Answers (6)

Saurabh Yadav
Saurabh Yadav

Reputation: 3386

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;

Upvotes: 0

deepika yadav
deepika yadav

Reputation: 91

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;

Upvotes: 3

S--
S--

Reputation: 398

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

Upvotes: 1

Shyam Kumar
Shyam Kumar

Reputation: 158

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

Upvotes: 0

Sharikov Vladislav
Sharikov Vladislav

Reputation: 7269

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

Upvotes: 8

Shiv Kumar Baghel
Shiv Kumar Baghel

Reputation: 2480

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

Upvotes: -1

Related Questions