javascript - JS getting value of object with key starting with a string -


is there quick way value of key starting string?

example :

var obj = {   "key123" : 1,   "anotherkey" : 2 }  obj['key1'] // return 1 obj['ano'] // return 2 

thanks

you can create helper function

function findvaluebyprefix(object, prefix) {     (var property in object) {     if (object.hasownproperty(property) && property.tostring().startswith(prefix)) {         return object[property];     } }  findvaluebyprefix(obj, "key1"); 

as kenney commented, above function return first match.


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -