0
const result = {
    "MEN": [],
    "WOMEN": [],
    "KIDS TEMP": [
        "BCDEFHJJJJJJ34EEE234",
        "ABCDEFGHIJKL12345678"
    ]
}
const result = {
    "MEN": [],
    "WOMEN": [],
    "CHILDREN TEMP": [
        "BCDEFHJJJJJJ34EEE234",
        "ABCDEFGHIJKL12345678"
    ]
}

I have an object result like above. Sometimes, it may have KIDS TEMP property and sometimes CHILDREN TEMP. Since this property has spaces, I cannot use dot operator to fetch these properties. I want to create a flag if either of KIDS TEMP or CHILDREN TEMP exist. I tried below:-

const part = 'KIDS TEMP' || 'CHILDREN TEMP';
    let check;
    if (result) {
      check = !!(result?.MEN.length !== 0 || result[part].length !== 0);
    }

But the above code breaks when I receive CHILDREN TEMP saying that it cannot read length of undefined. How do I fix this?

Neha Chaudhary
  • 1,071
  • 4
  • 15
  • 31
  • 2
    You need to do both checks separately. `const part = 'KIDS TEMP' || 'CHILDREN TEMP';` will not magically run two checks later, it will simply store `'KIDS TEMP'` in `part`. What you can do is use an array: `const parts = ['KIDS TEMP', 'CHILDREN TEMP']` then use `parts.some(part => result[part] && result[part].length)` –  Apr 06 '22 at 08:45
  • [How do I check if an object has a specific property in JavaScript?](https://stackoverflow.com/questions/135448/how-do-i-check-if-an-object-has-a-specific-property-in-javascript) – Andreas Apr 06 '22 at 08:45
  • `I cannot use dot operator to fetch these properties`, you can access the property even if it has spaces or reserved keywords in the key. You just need to use the square brackets in place of the dot: `result['CHILDREN TEMP']` – Christian Vincenzo Traina Apr 06 '22 at 08:50

2 Answers2

1

Object.keys() returns the property names of an object. You can check like this, if an object possesses a certain property:

const result = {
    "MEN": [],
    "WOMEN": [],
    "KIDS TEMP": [
        "BCDEFHJJJJJJ34EEE234",
        "ABCDEFGHIJKL12345678"
    ]
}

console.log(Object.keys(result).includes("KIDS TEMP"));
console.log(Object.keys(result).includes("CHILDREN TEMP"));

If you want to test, if a certain attribute exists, and in case it exists, contains a non-empty array, you can test it like this:

const result = {
    "MEN": [],
    "WOMEN": [],
    "KIDS TEMP": [
        "BCDEFHJJJJJJ34EEE234",
        "ABCDEFGHIJKL12345678"
    ]
}

console.log(result["KIDS TEMP"]?.length > 0);
console.log(result["CHILDREN TEMP"]?.length > 0);

The main problem with your code is that const part = string1 || string2 always evaluates to string1, which is not what you intend to do.

Mario Varchmin
  • 3,704
  • 4
  • 18
  • 33
1

You can use Object's method hasOwnProperty it will return true/false depends on if property exists in object or not.

const result = {
    "MEN": [],
    "WOMEN": [],
    "KIDS TEMP": [
        "BCDEFHJJJJJJ34EEE234",
        "ABCDEFGHIJKL12345678"
    ]
}

console.log(result.hasOwnProperty('KIDS TEMP'),result.hasOwnProperty('CHILDREN TEMP'));
RA17H45M40S
  • 113
  • 1
  • 6