Thursday, July 26, 2018

Javascript For each json




I'm making a call via ajax javascript to a page (ASP) that returns a json
with which I would then go to a select value




the json that I returned is of this type



{
"options": {
"1": "All locations",
"2": "Egypt",
"3": "El Alamein",
"4": "Marsa matrouh",
"5": "Sharm el sheikh "

}
}


or



{
"options": {
"1": "All locations",
"2": "Abu dhabi",

"3": "Dubai"
}
}


I don't know the length and the elements that are contained
then I would make a loop that allows me to extract the values which then will insert the value and label



I'm looking at the net but the examples shown are always with the field name to be recalled that I could not learn about not knowing the length




could someone kindly show me how this cycling json


Answer



var obj = {"options": {"1": "All locations", "2": "Egypt", "3": "El Alamein", "4": "Marsa matrouh", "5": "Sharm el sheikh "}}

for(var item in obj.options) {
console.log(obj.options[item]);
}

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...