0

I have the following data structure:

var map_neighbours = [{
    "Alaska": ["UstKamchatsk", "Yukon"]
}, {
    "Algeria": ["Chad", "Egypt", "SierraLeone", "Spain"]
}, {
    "AntarticWildlifeTerritory": ["AustralianAntarticTerritory", "SouthAfricanAntarticTerritory"]
}, .....]

The user selects a region via the page, I want to loop through this structure, find the region, then loop through the sub-regions (in the corresponding position).

So for example, for Algeria I want to get "Chad", "Egypt", "SierraLeone", "Spain" one by one out of a loop.

I have tried a few variation of this without success (region is supplied by the user as mentioned above):

var neighbourArray = map_neighbours[region];

$.each(neighbourArray, function(idx, val) {

    console.log("Neighbours= " + neighbourArray[region][idx]);

});

or

$.each(map_neighbours, function(outer, val) {

    if (map_neighbours[outer] == region) {

        neighbourArray = (map_neighbours[outer][]);

        $.each(neighbourArray, function(inner, val) {

            console.log("Neighbours= " + neighbourArray[outer][inner]);

        });

     );

};

Thanks for any advice.

1
  • Is that = > supposed to be there, or is it supposed to be a :? Commented Oct 8, 2015 at 3:25

1 Answer 1

2

With your current structure you need to iterate over the array and see whether the item has the input value as a key like

var map_neighbours = [{
  "Alaska": ["UstKamchatsk", "Yukon"]
}, {
  "Algeria": ["Chad", "Egypt", "SierraLeone", "Spain"]
}, {
  "AntarticWildlifeTerritory": ["AustralianAntarticTerritory", "SouthAfricanAntarticTerritory"]
}];

var input = 'Algeria',
  result;
$.each(map_neighbours, function(i, item) {
  if (item[input]) {
    result = item[input];
    return false;
  }
})

if (result) {
  snippet.log(JSON.stringify(result));
} else {
  snippet.log('not found')
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>


But a much better structure to handle the case is to use a key value object instead of an array of objects

var map_neighbours = {
  "Alaska": ["UstKamchatsk", "Yukon"],
  "Algeria": ["Chad", "Egypt", "SierraLeone", "Spain"],
  "AntarticWildlifeTerritory": ["AustralianAntarticTerritory", "SouthAfricanAntarticTerritory"]
};

var input = 'Algeria',
  result = map_neighbours[input];

if (result) {
  snippet.log(JSON.stringify(result));
} else {
  snippet.log('not found')
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

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

Comments

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.