Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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' || '*');

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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] === "*"
);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading