Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

117
Views
How do I check the missing fields while comparing an array and an object?

I have an string array that has the name of some particular fields as shown :

const fields = [
    "FULL_NAME",
    "EMAIL_ADDRESS",
    "PHONE_NUMBER",
    "HOME_ADDRESS"
]

and the object that is supposed to store the state for these fields as :

const obj = {
    "FULL_NAME": {
        firstName: "John",
        lastName: "Doe"
    },
    "PHONE_NUMBER": ""
}

Notice how some of the fields compared to the array are missing, like "EMAIL_ADDRESS" and "HOME ADDRESS".

I want to somehow compare these two and want the function to return true only if all the fields mentioned in the 'fields' array are present in the data object 'obj' WITH SOME DATA present in them(str.length > 0).

How do I accomplish this?

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

You can use Array.every function for your usecase.

const hasAllFieldsIn = (fields, obj) => {
  const keys = Object.keys(obj);
  return fields.every(item => keys.indexOf(item) >= 0 && !!obj[item]);
}

const fields = [
    "FULL_NAME",
    "EMAIL_ADDRESS",
    "PHONE_NUMBER",
    "HOME_ADDRESS"
]

const obj = {
    "FULL_NAME": {
        firstName: "John",
        lastName: "Doe"
    },
    "PHONE_NUMBER": ""
}

const obj2 = {
    "FULL_NAME": {
        firstName: "John",
        lastName: "Doe"
    },
    "PHONE_NUMBER": "",
    "EMAIL_ADDRESS": "a@b.com",
    "HOME_ADDRESS": ""
}

const obj3 = {
    "FULL_NAME": {
        firstName: "John",
        lastName: "Doe"
    },
    "PHONE_NUMBER": "837362638",
    "EMAIL_ADDRESS": "a@b.com",
    "HOME_ADDRESS": "zz"
}

console.log(hasAllFieldsIn(fields, obj))
console.log(hasAllFieldsIn(fields, obj2))
console.log(hasAllFieldsIn(fields, obj3))

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!