Search code examples
javascriptescapingjavascript-objects

Escape the asterisk, when used in a string (javascript)


I am trying to search for any object key that has the value of "*" Not the wildcard character, but the string "*".

How do I escape the asterisk in the following code? currently (and rightly so) this returns every property in the object.

keys = Object.keys(fullListObj).filter((k) => fullListObj[k] === 'n/a' || '*');

Solution

  • I think that your error was that you forgot to add this check fullListObj[k] === "*" What you did is you asked if fullListObj[k] === "n/a" or "*" and "*" is a string that is not empty so this will always evaluate to true.

    You should change it to this:

    keys = Object.keys(fullListObj).filter(
      (k) => fullListObj[k] === "n/a" || fullListObj[k] === "*"
    );