skip to Main Content

In Typescript

i need the object in key but in my case, i need the specific key like key is jonnny so i find the last word nny to find this key so it possible to find this key and this is only one key this type so how to find this key please help and give me the best way and short using build function

I found all the articles but did not get this type of information

2

Answers


  1. Object.keys() will get you a list of keys, and .find() will pull the first matching item from a list, so you could write

    const matchingKey = Object.key(myObject).find(key => key.startsWith('onny'));
    
    Login or Signup to reply.
  2. I think the first answer gets it mostly correct, except if you’re looking to find a key that ends with something you’d use endsWith

    const matchingKey = Object.key(myObject).find(key => key.endsWith('nny'));
    

    EDIT

    If you want an exclusion/"not using" based logic, here’s an example –

    # Note the ! before key.endsWith(...    
    const matchingKey = Object.key(myObject).find(key => !key.endsWith('nny'));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search