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

android - net_scheduler holding wakelock -

sql - MySQL : Getting Entries from a many-to-many table -

java - Retrieving data from database using jsp (Hibernate + Spring + Maven) -