0

I have an array of nested objects:

const array =[
 { 
    "id": 1,
    "time": { 
       "timezone": "2021-09-22T05:36:22.484Z"
       "city": "Perth"
       "country:: "Australia
       "date": "2021/10/10"
    }
 },
 {
   ​"id": 2,
   ​"time": { 
       "timezone": ​"2021-10-22T03:25:26.484Z"
       "city": ""
       "country: "Americas"
       "date": "2021/10/10"
    }
 },
 {
   ​"id": 3,
   ​"time": { 
       "timezone": ​"2021-09-27T02:43:26.564Z"
       "city": ""
       "country: ""
       "date": "2021/10/10"
    }
 }];

I want to check each value in the time object to see if there exists an empty string without having to have multiple || statements.

What I have tried using lodash:

if(array.find((k)=> _.isEmpty(k.timezone)) || array.find((k)=> _.isEmpty(k.city)) || array.find((k)=> _.isEmpty(k.country)) || array.find((k)=> _.isEmpty(k.date))) {
//do something
 
 } else {
//do something else
 }

This seems to do the trick but trying to find a succinct and cleaner way to do this as there could be more values in the time object, preferably in es6.

0

1 Answer 1

1

Check if .some of the Object.values of any of the time subobjects includes the empty string.

if (array.some(
  k => Object.values(k.time).includes('')
)) {
  // do something
} else {
  // do something else
}
Sign up to request clarification or add additional context in comments.

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.