M
M
mr_blaze2017-07-09 14:55:19
JavaScript
mr_blaze, 2017-07-09 14:55:19

How to check if an associative array contains a certain JavaScript value?

Hello!
How to check if array {1: "", 2: "123", 3:"",...} contains empty values?
Thanks to all!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Sokolov, 2017-07-09
@mr_blaze

Iterate through each of the properties and check. If it evaluates to false when converted to boolean, then it is empty.
There are two exceptions: a true boolean falseand a number 0, which will not be considered null.

var data = {1: "", 2: "123", 3:""};

function anyEmpty(obj) {
  var p, v;
  for( p in obj) {
    v = obj[p];
    if( !obj.hasOwnProperty(p)) continue;
    if( v === false || v === 0) continue;
    if( !v ) return true;
  }
  return false;
}

anyEmpty(data)  // true

A
Andrew, 2017-07-09
@undefined_title

in js {} is an ordinary object, in this language objects play two roles, as data carriers and how an ordinary object will return true if there is at least one empty value, null, undefined, "", false and so on.
will return the number of empty values

E
eeiaao, 2017-07-09
@eeiaao

for..in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question