javascript - access elements in array of strings that contain certain strings -



javascript - access elements in array of strings that contain certain strings -

i have array can this: ["whatwp", "isvbz", "thedt", "temperaturenn", "inin", "bostonnn"]

i want access element containing in, if exists , next elements(s) until reach , including element nn in it. , bring together elements string.

when seek access element containing in so, -1 there no element containing in.

here's how trying it:

strarr = ["whatwp", "isvbz", "thedt", "temperaturenn", "inin", "bostonnn"]; strarr[strarr.indexof('in')];

but undefined because nil @ -1 exists.

how can access element of array of strings if contains in , every element after until matches element containing nn, including element? , joining string?

you need loop that:

var strarr = ["whatwp", "isvbz", "thedt", "temperaturenn", "inin", "bostonnn"]; var foundstr = null; (var = 0, cstring; < strarr.length; ++i) { cstring = strarr[i]; if (cstring.indexof("in") !== -1) { foundstr = cstring; break; } } if (foundstr !== null) { /* someting found string */ }

strarr[strarr.indexof('in')] returning weird value because:

strarr.indexof('in') // returns -1 (no string "in" in array) strarr[-1] // undefined

javascript jquery arrays

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -