Total JSON noob here. I\'m trying to cycle through some JSON to pull out the first image from an array inside the object, and after 4 hours of having at it, I\'ve decided I
You can't just search through every field with a partial match, so you'll have to iterate through every field and then check the field names for the match. Try something like this:
var json = {
"content_0_subheading": [
"Title text"
],
"content_1_text": [
"Some text"
],
"content_2_image": [
[
"http://staging.livelivelyblog.assemblo.com/wp-content/uploads/2013/09/wellbeing-260x130.jpg",
260,
130,
true
]
],
"content_2_caption": [
""
]
}
for (var key in json) {
if (json.hasOwnProperty(key)) {
if (/content_[0-9]+_image/.test(key)) {
console.log('match!', json[key]); // do stuff here!
}
}
}
Basically, what we're doing is:
1) Loop through keys of json object for (var key in json)
2) Ensure the json has the property, and we're not accessing keys we don't want if (json.hasOwnProperty(key))
3) Check if key matches the regular expression /content_[0-9]+_image/
3a) Basically, test if it matches content_ANY NUMBERS_image where ANY NUMBERS is equal to at least one digit or more
4) Use that data however you please console.log(json[key])
Hope this helps!