1

To create a function to determine an object's property is empty or contain double quote as below

Code## Heading ##

private ReturlFalseIfObjectPropertiesEmpty(a) {
    Object.getOwnPropertyNames != undefined ? Object.getOwnPropertyNames(a).length != 0 : (
      function() {
         for (const key in a) return (key != undefined) && (key != "");
      }) ();
    }

Data that I need to validat

Heading

const data = [{"employeeContact": {
        "personalPhone": "",
        "workPhone": "",
        "personalEmail": "",
        "workEmail": "",
        "currentAddress": "",
        "permanentAddress": ""
      }}
    ];

1 Answer 1

3

You can try to check for an empty value or an empty string in an Object through these suggested methods:

NOTE: All of these results shows a new an array of object with properties that doesn't have an empty value or ''

If you would like to have its result otherwise, showing the fields that has an empty value or '', you can change the conditions below from value != '' to value == '' to give you your desired results.

Sample Data

const data = [{"employeeContact": {
    "personalPhone": "",
    "workPhone": "",
    "personalEmail": "",
    "workEmail": "",
    "currentAddress": "sample",      // Supplied one with a value
    "permanentAddress": ""
  }}
];

Method #1 - Use the ES7 For Loop with Object.entries

const employeeContract = data[0].employeeContact;
let result = [];

for (const [key, value] of Object.entries(employeeContract)) {
  if (value != '') result.push({ [key]: value });   // This will push a new object with its existing key and assign its value if the object is not empty. Ex. { personalPhone: 'sample'}
}

console.log(result);   // [ { currentAddress: 'sample' } ]

Method #2 - Filter with ES7 Object.entries (Simplified)

const employeeContract = data[0].employeeContact;
const result = Object.entries(employeeContract).filter(([key, value]) => value != '' ? { [key]: value } : null);

console.log(result);   // [ { currentAddress: 'sample' } ]

To use Object.entries in Typescript, you will need to edit your tsconfig.json

{
  "compilerOptions": {
    "target": "es2017",        // Use es2017 to use all of its features
    "module": "commonjs",
    "strict": true,
    "esModuleInterop": true
  }
}

// Or if you are using ES6 and you only want to use the ES7 Object.entries() feature then, just supply values on the "lib" property
{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "strict": true,
    "esModuleInterop": true,
    "lib": ["es6", "dom", "es2017.object"]   // Add this line to enable using Object.entries()
  }
}

Hope this helps

Sign up to request clarification or add additional context in comments.

2 Comments

.entries does not existing Typescript
@HengSopheak had updated my comment on how to enable Object.entries on your ts files through editing the target or lib property in tsconfig.json

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.