Home > Mobile >  Search for a key (that's an invalid character) in an object
Search for a key (that's an invalid character) in an object

Time:09-29

I have some characters as object keys in an object like: 

Those are actually icons, and I want to replace one icon with the other.

And my icons object is laid out like this:

icons = {
    : {old: , new: }, 
    : {old: , new: }
}

How can I search for the icon? When I try to search, I get an invalid character error.

enter image description here

CodePudding user response:

When you declare your object, put quotes around the keys:

const icons = {
  "": { ... } // and so on
};

Then you can access it using the bracket notation:

icons[""]
  • Related