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.

3
  • Possible duplicate of javascript - check if object is empty Commented May 30, 2018 at 2: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') Commented May 30, 2018 at 2: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? Commented May 30, 2018 at 2:34

3 Answers 3

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

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
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,
  },
]));

4 Comments

would typeof item.office_id === 'number' be an appropriate test instead?
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)
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)
ahhh, yes of course - I was looking at the OP's code with blinkers on :p
0

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

const result = yourArray.some(item => item !== '')

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.