2

I have this array of objects.

[Object, Object, Object]
0:Object
 name: "Rick"
 Contact: "Yes"
 Date:'null'
 Location:'null'
1:Object
 name:"Anjie"
 Contact:"No"
 Date:'13/6/2016'
 Location:'LA'
2:Object
 name:"dillan"
 Contact:"Maybe"
 Date:'17/6/2016'
 Location:'NY'

As you can see, there are null values for Object[0] for Date and Location. I want to check if there is a null value present in the entire array of Objects. If there is a null value present for 'Date' and 'Location', i should be able to display 'Null present' at the console. If no null values are present, it should display 'Data right' at the console.

can someone please let me know how to achieve this.

2
  • 1
    is 'null' of type string or null? What do you want to return when there is a null value present, but not for 'Date' or 'Location'? Commented Jun 24, 2016 at 17:48
  • How did the nulls go from null to strings? Was not Ninja edited from when I first saw the question? Commented Jun 24, 2016 at 17:56

4 Answers 4

6

Do it using Object.keys() and Array#some methods

var data = [{
  name: "Rick",
  Contact: "Yes",
  Date: null,
  Location: null
}, {
  name: "Anjie",
  Contact: "No",
  Date: '13/6/2016',
  Location: 'LA'
}, {
  name: "dillan",
  Contact: "Maybe",
  Date: '17/6/2016',
  Location: 'NY'
}];


// iterate over array elements
data.forEach(function(v, i) {
  if (
    // get all properties and check any of it's value is null
    Object.keys(v).some(function(k) {
      return v[k] == null;
    })
  )
    console.log('null value present', i);
  else
    console.log('data right', i);
});

Sign up to request clarification or add additional context in comments.

1 Comment

Array.prototype.some() comes up as logical but i have believe findIndex turns out to be much faster somehow.
5
var wasNull = false;
for(var i in objectsArray) {
  if(objectsArray[i].Date == null || objectsArray[i].Location == null) wasNull = true;
}
if(wasNull) console.log('Was null');
else console.log('Data right');

1 Comment

thanks for the simplest and the answer i was looking for
2

Use some() and check if there is a null.

var arr = [
   { a : "a", b : "b", c : "c" },
   { a : "a", b : "b", c : "c" },
   { a : "a", b : "b", c : null }
];

function hasNull(element, index, array) {
  return element.a===null || element.b===null || element.c===null;
}
console.log( arr.some(hasNull) );

If you do not want to hardCode the if, than you need to add another loop and loop over the keys.

var arr = [
   { a : "a1", b : "b1", c : "c1" },
   { a : "a2", b : "b2", c : "c2" },
   { a : "a3", b : "b3", c : null }
];

function hasNull(element, index, array) {
  return Object.keys(element).some( 
    function (key) { 
      return element[key]===null; 
    }
  ); 
}
console.log( arr.some(hasNull) );

or JSON with a reg exp (

var arr = [
   { a : "a1", b : "b1", c : "c1" },
   { a : "a2", b : "b2", c : "c2" },
   { a : "a3", b : "b3", c : null }
];

var hasMatch = JSON.stringify(arr).match(/:null[\},]/)!==null;
console.log(hasMatch);

Comments

2

A solution using underscore:

var nullPresent = _.some(data, item => _.some(_.pick(item, 'Date', 'Location'), _.isNull));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.