Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have this Javascript variable
C#
var City ={
    "Cairo": [
        {
            "name": "Place 1"
        },
        {
            "name": "Place 2"
        },
        {
            "name": "Place 3"
        }
    ],
    "Giza":[{"name":"My Place 1"},{"name":"My Place 2"},{"name":"My Place 3"}]
}


I want to loop through it, to get the key name (Cairo, Giza) and then loop through each key to get its data

C#
for (i=0;i < City.Length;i++)
{    
    data=Object.Keys(City)[i];
    for (s=0;s < data.Length; s++)
    {
      value=data[s].value;
    }
}


I need help with this. Thank you

--Update
I can get the keys but I get nothing from the second loop to get each keys value. This loop does not work
C#
for (s=0;s < data.Length; s++)
{
    value=data[s].value;
}
Posted
Updated 23-Aug-15 8:06am
v2
Comments
Patrice T 23-Aug-15 9:59am    
What is your problem ?
What is the question ?
elshorbagy 23-Aug-15 10:25am    
I can get the keys but I get nothing from the second loop to get each keys value. This loop does not work
for (s=0;s < data.Length; s++)
{
value=data[s].value;
}
Sergey Alexandrovich Kryukov 23-Aug-15 12:30pm    
Your structure is 3-level, not 2-level. The problem is very simple.
I answered the question, please see Solution 1.
—SA

Your little problem is that you use different key for the city, its name. Consider this:
JavaScript
var cities = {
    "Cairo": [
       { "name": "Place 1" },
       { "name": "Place 2" },
       { "name": "Place 3" }],
    "Giza":[
       {"name":"My Place 1"},
       {"name":"My Place 2"},
       {"name":"My Place 3"}]
}

for (city in cities)
    for (var index=0; index < cities[city].length; ++index)
        alert(city + ": place name: " + cities[city][index].name)

Are you getting the idea?

Your structure is 3-level, not 2-level. It's not "city", it's a set of cities. Each city has something you could call "places", also a set, but expressed as an array. And finally, this is an array of objects, not string, only each object has the only property "name".

—SA
 
Share this answer
 
v6
Comments
Maciej Los 23-Aug-15 18:12pm    
Sergey, it's really, really rarely when you provide some piece of code. Why you've made exception?
+5!
Sergey Alexandrovich Kryukov 23-Aug-15 20:21pm    
Thank you, Maciej.

I don't think it's too rare. Why in this case? Because it's fast: I created the program I published in my article, "JavaScript calculator", which makes such exercises very easy, thanks to some debugging features I created, and also handling of lexical errors as exception. Please see the this section: 5. Handling Lexical Errors. Please see, you may find it pretty interesting.

Another reason is this: as you can see, the "new" code in my sample is just 3 lines :-)

—SA
Maciej Los 24-Aug-15 2:10am    
Well... I didn't say it's too rare. I said: seldom if ever. Most of your answers (let me guess: 99,9%) does not contain any piece of code. In my opinion it's very good practice.
Sergey Alexandrovich Kryukov 24-Aug-15 8:45am    
Whatever...
I think, ideally, most answers, some number over 50%, would be with some code. It just doesn't happen because so many questions are inadequate to the purpose of this forum...

Thank you again.
—SA
My solution: This not a specific solution to your problem, this is a general solution when your code don't do what it should.

Your problem is that your code don't do what you think it should, and you don't understand why.

The Solution is to use the debugger, its a marvellous tool that allow you to see step by step what your code is really doing, pay particularly attention to the variables.

For today's problem, you may discover that the variable data do not contain what you think it is.
 
Share this answer
 
Comments
Patrice T 24-Aug-15 2:01am    
To downvoter:
It is like in the saying,
"Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime." Maimonides
a specific solution is like giving a man a fish.
using the debugger is like teaching him how to fish.
May be not so bad !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900