1

I have an array that looks like below

process:Array[3]
0:Object
    office_id:""
1:Object
    office_id:6
2:Object
   office_id:""

I want to check if office_id named key is not empty

If I find at least one office_id that is not empty it will return true else if all is empty it will return false.

The default value of office_id is empty string as you can see.

The object is dynamic since I'm using some input select form to add another object with office_id so if they select something another object will be added with office_id equal to that specific select id.

Now, as for validation purpose I need to check if the object process contains an office_id with a numeric value.

Ikong
  • 2,540
  • 4
  • 38
  • 58
  • Possible duplicate of [javascript - check if object is empty](https://stackoverflow.com/questions/42813784/javascript-check-if-object-is-empty) – Ru Chern Chong May 30 '18 at 02:31
  • none of those objects are empty, they all have a propery called `office_id` - though, reading between the lines ... `process.some(i => i.office_id)` will be true if at least one object in the `process` array has a non-falsey `office_id` property - however, an `office_id:0` would not count as "truthy" ... so .. perhaps ... `process.some(i => typeof i.office_id === 'number')` – Jaromanda X May 30 '18 at 02:32
  • `[{office_id:""}, {office_id:""},{office_id:""}].some(obj => typeof obj.office_id === "number")`, but you should first decide what "office_id is empty" actually means: empty string only? missing? undefined? null? 0? NaN? date? – Nickolay May 30 '18 at 02:34

3 Answers3

1

Using simple for loop and more efficient way

function () {
  for(i = 0; i < process.length; i++){
    if (process[i].office_id){
      return true;
      break;
    }
  }

  return false;
}

Using ES5 filter

function () {
  var filteredProcess = process.filter(function(pr) {
    if (pr.office_id){
      return true;
    }
  });

  return filterProcess.length ?
    true: 
    false;
}
Geuis
  • 41,122
  • 56
  • 157
  • 219
ShibS
  • 40
  • 7
  • `if(filteredProcess.length) {return true;} else{ return false }` ... or `return !!filteredProcess.length` (possibly don't even need the `!!` if `truthy`/`falsey` is good enough as opposed to `true`/`false` – Jaromanda X May 30 '18 at 02:41
0

You can use some combined with the javascript truthy-ness to determine if it's empty. Since 0 evaluates to falsey, you need to handle that as a special case. See isEmpty below

const isEmpty = (arr) => arr.some(item => item.office_id === 0 || !!item.office_id);

// This returns false because all the elements are falsey
console.log(isEmpty([
  {
    office_id: "",
  },
  {
    office_id: undefined,
  },
  {
    office_id: null,
  },
  {
    office_id: "",
  },
]));

// This returns true because the number 6
console.log(isEmpty([
  {
    office_id: "",
  },
  {
    office_id: 6,
  },
]));

// This returns true because 0 is special cased
console.log(isEmpty([
  {
    office_id: "",
  },
  {
    office_id: 0,
  },
]));
AnilRedshift
  • 7,937
  • 7
  • 35
  • 59
  • would `typeof item.office_id === 'number'` be an appropriate test instead? – Jaromanda X May 30 '18 at 02:36
  • but wouldn't `typeof item.office_id === 'number'` always be the same truthy/falsey as your condition? (just thought it was simpler, that's all - your logic is fine) – Jaromanda X May 30 '18 at 02:39
  • No, `"foo"` would return `true` in my code, and `false` in the typeof check. (Which isn't to say that one is more right than the other, depends on what is desired) – AnilRedshift May 30 '18 at 02:39
  • 1
    ahhh, yes of course - I was looking at the OP's code with blinkers on :p – Jaromanda X May 30 '18 at 02:42
0

Use Array.prototype.some to verify that at least one member of the array matches your condition:

const result = yourArray.some(item => item !== '')
djfdev
  • 5,747
  • 3
  • 19
  • 38