-1

First see how JSON should look like

[
    {"ShortCode":"US","Name":"United States"},
    {"ShortCode":"CA","Name":"Canada"},
    {"ShortCode":"AG","Name":"Antigua and/or Barbuda"}
]

Code:

var countries = [];
map = {};

// This is going to make an HTTP post request to the controller
return $.post('/Client/CountryLookup', { query: query }, function (data) {
    // Loop through and push to the array
    $.each(data, function (i, country) {
    map[country.Name] = country;
    countries.push(country.Name);
});

$post() will return the above json & i need to parse the json in each loop but i do not understand what will be store in map object this line map[country.Name] = country;

Suppose country name is "united state" so actually store will be map['united state']=country what does it mean?

Even in the save code map{} access later like

var selectedShortCode = map[item].ShortCode;

How map can have a property like ShortCode ??

So please discuss this type of coding technique in detail and help me to understand the above code with few more example. thanks

5
  • 1
    Welcome to the wonderful world of async! You can't do that. Commented Jun 20, 2014 at 19:03
  • You're setting an index (the country name) on an object to be equal to the object containing the country name. So console.log(map['United States'].Name) will output United States. Commented Jun 20, 2014 at 19:06
  • @SLaks why not? Misled by lack of indentation? Commented Jun 20, 2014 at 19:41
  • @CupawnTae: He's trying to access map after the callback. Commented Jun 20, 2014 at 19:50
  • @SLaks possible, but that's not evident from the question - it depends on how he's using the return value from his function - the return value will be a Promise, and if he puts his code in promise.done() it'll work just fine. Good point though! Commented Jun 20, 2014 at 20:04

2 Answers 2

1

your map is an object literal defined also known as hash or dictionary or relational array in other languages what basically relates a key(Strin) with a value(anything) in this case relates country names with the actual country object thats why

the map structure after your map[country.Name] = country operations would be { "United States":{"ShortCode":"US","Name":"United States"}, "Canada":{"ShortCode":"CA","Name":"Canada"}, "Antigua and/or Barbuda":{"ShortCode":"AG","Name":"Antigua and/or Barbuda"} }

then when you do

map["United States"]

you get

{"ShortCode":"US","Name":"United States"}

Sign up to request clarification or add additional context in comments.

2 Comments

but how we can access like this way map[item].ShortCode? how ShortCode property used here ? please come with some sample code to understand this dynamic property ? thanks for reply.
thats what is writtend in the answer using map[item] is the same as using map["United States"] if item=="United States" map["United States"] is an object containing the properties ShortCode and Name hence map["United States"].ShortCode is "US".
0

Your data after the $.post call would be:

data = 
[
    { ShortCode: "US", Name: "United States" },
    { ShortCode: "CA", Name: "Canada" },
    { ShortCode: "AG", Name: "Antigua and/or Barbuda" }
];

Take a look at the jQuery's .each function documentation.

It basically can be rewritten as follows:

for(var i in data)
{
    var country = data[i];
    map[country.Name] = country;
    countries.push(country.Name);
}

So you end up with:

map =
{
    "United States": { ShortCode: "US", Name: "United States" },
    "Canada": { ShortCode: "CA", Name: "Canada" },
    "Antigua and/or Barbuda": { ShortCode: "AG", Name: "Antigua and/or Barbuda" }
};

countries = [ "United States", "Canada", "Antigua and/or Barbuda" ];

So your code parses json, and outputs all the countries that exist (countries) and all the data associated with them (map), in two different objects. countries can be used to easily itterate through the map, but there's no real need for it.

EDIT:

var country = data[0];
// country = { ShortCode: "US", Name: "United States" };
map[country.Name] = country;
// map["United States"] = { ShortCode: "US", Name: "United States" };

So you can later take map["United States"], it will return you an object { ShortCode: "US", Name: "United States" }, then you can simply access it's property ShortCode if you'd like to.

Mind that all variables in JavaScript are passed by reference, and that myObject["someProperty"] is exactly the same as myObject.someProperty.

2 Comments

but how we can access like this way map[item].ShortCode? how ShortCode property used here ? please come with some sample code to understand this dynamic property ? thanks for reply.
Edited. Hope it helps you understanding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.