0

Hello i have this array :enter image description here What is the best approach to extract region[],city[] and area[] considering that this array could grow like:region5, city5,area5,region6,city6,area6?

Thank you

6
  • Use an array of objects, not an array of strings. Commented Feb 9, 2012 at 16:37
  • Not sure exactly what you mean. You want to extract all "cityX" elements to city[] array, all "areaX" elements to area[] area, etc? Commented Feb 9, 2012 at 16:37
  • Please provide code here, not on an image. Commented Feb 9, 2012 at 16:38
  • yes exactly MeLight, thank you Commented Feb 9, 2012 at 16:38
  • thank you Jivings but i don't have the code , i just need to get this object from page and extract him as MeLight said, sorry about this Commented Feb 9, 2012 at 16:40

3 Answers 3

0

I would do something like this :

function getData(array){

    var cities = [], regions = [], areas = [],
        item, i, j, value, 
        hash = array.slice(5, array.length); //we don't need the first 6 values. 
                                 //assuming the data always comes in this order.

    hash.sort();//to get values in the right order
    for(i= 0, j = hash.length; i<j; i++){

        item = hash[i];
        value = item.split("=")[1];

        if(item.indexOf("a"=== 0)){
            areas.push(value);
        }
        else if(item.indexOf("c"=== 0)){
            cities.push(value);
        }else if(item.indexOf("r"=== 0)){
            regions.push(value);
        }

    }

    return {
        areas : areas,
        cities : cities,
        regions : regions
    };
}
0

Here's the way I would do it:

var strings = ["city1=Bellevue", "city2=Seattle", "area1=Sound", "area2=Boston"];
var keys = {};

for(var i = 0; i < strings.length; i++)
{
   var parse = /^([a-z]+)[0-9]+\=([a-z]+)$/i
   var result = parse.exec(strings[i]);
   var key = result[1];
   var value = result[2];
   if(!keys[key]) keys[key] = [];
   keys[key].push(value);
}

alert(keys['city']);
alert(keys['area']);

This assumes the order of the keys doesn't matter (I ignore the number at the end of each key). You could, of course, modify the code to parse out that number and use it as an array index as well. I'll leave this up to you, hoping this will at least get you started.

0
    var info = ["cities1=Bellevue", "cities2=Seattle", "areas1=Sound", "areas2=Boston", "regions1=Region1", "regions2=Region2"];


    var cities = [];
    var areas = [];
    var regions = [];

    for(var i = 0; i < info.length; i++){
        var item = info[i];
        if(/cities\d+/.test(item)){
            cities.push(item.split("=")[1]);
        }
        else if(/areas\d+/.test(item)){
            areas.push(item.split("=")[1]);
        }
        else if(/regions\d+/.test(item)){
            regions.push(item.split("=")[1]);
        }
    }

Assuming the array will be always like the example shown. Otherwise please add condition checks where the item is split.

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.