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
Post a Comment