javascript - how to get Index of json object -
i have json string as:
var str="[{ 'label': 'month'},{ label: 'within'},{ label: 'from'}, { label: 'where'},]";
i converted json objects as:
var tagstring = eval(str);
i want index of month without loop
is there better way index of object without using loops?
thanks in advance!
if labels, change structure this, it's "an array of labels" which, in opinion, more proper.
var str = '["month","within","from","where"]';
then parse them json.parse
, or since using jquery, $.parsejson
work on more browsers:
var labels = json.parse(str);
labels
should array, can use array.indexof
.
var index = labels.indexof('month');
it's es5 , modern browsers support it. older browsers, you need polyfill unfortunately... uses loop.
Comments
Post a Comment