2

I would like to create an array from a multidimensional array like this:

var dataItaly = [
/*Town       Region   City*/
  ["Castelspina","Piemonte","Alessandria"  ],
  ["Cavatore","Piemonte","Alessandria"  ],
  ["Cella Monte","Piemonte","Alessandria"  ],
  ["Cereseto","Piemonte","Alessandria"  ],
  ["Cantarana","Piemonte","Asti"  ],
  ["Capriglio","Piemonte","Asti"  ],
  ["Casorzo","Piemonte","Asti"  ],
  ["Albino","Lombardia","Bergamo"  ],
  ["Algua","Lombardia","Bergamo"  ],
  ["Abbiategrasso","Lombardia","Milano"  ],
  ["Agrate Brianza","Lombardia","Milano"  ],
  ["Aicurzio","Lombardia","Milano"  ],
  ["Almese","Piemonte","Torino"  ],
  ["Alpette","Piemonte","Torino"  ],
  ["Alpignano","Piemonte","Torino"  ],
  ["Andezeno","Piemonte","Torino"  ],
  ["Albonese","Lombardia","Pavia"  ],
  ["Albuzzano","Lombardia","Pavia"  ],
  ["Arena Po","Lombardia","Pavia"  ],
  ["Badia Pavese","Lombardia","Pavia"  ]
];

this is my starting point ...

var region = "Piemonte"
// array must appear the city of "Piemonte"

function createArrayCity (dataItaly, region) {
var arrayList;
for (i = 0; i <dataItaly.length i) {
  if the region === [1] {
    arrayList [2]. append;
return arrayList;
}
}
}

The contents of this must be arrayList [Alessandria, Torino, Asti]. If var region = "Lombardia" the contents of arrayList is [Bergamo, Milano, Pavia]

1
  • Guys thanks a lot for the help. For me, the iterations in the array are still a tough nut. Now I have a base for studying Commented May 14, 2013 at 16:31

3 Answers 3

1

I'd do it using .map() and .filter():

var result = dataItaly.map(function(e) {
    return e[1] === region ? e[2] : null;
}).filter(function(e, i, a) {
    return e !== null && a.indexOf(e) === i;
});

N.B.: These methods are rather new and some old browsers might not support them. You should use shims (from here and here) to fix the compatibility.

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

Comments

1

You can use concat to merge arrays:

var dataItaly = [
  ["Castelspina","Piemonte","Alessandria"  ],
  ["Cavatore","Piemonte","Alessandria"  ],
  ["Cella Monte","Piemonte","Alessandria"  ],
  ["Arena Po","Lombardia","Pavia"  ],
  ["Badia Pavese","Lombardia","Pavia"  ]
];
var merged = [];
merged = merged.concat.apply(merged, dataItaly);

To remove duplicate (if you want that) you could do:

merged = merged.filter (function (v, i, a) { return a.indexOf (v) == i });

Comments

0

Try

function createArrayCity (dataItaly, region) {
    var arrayList = [];
    for (i = 0; i <dataItaly.length; i++) {
        if (dataItaly[i][1] === region && indexOf(arrayList, dataItaly[i][2]) == -1) {
            arrayList.push(dataItaly[i][2])
        }
    }
    return arrayList;
}

function indexOf(array, obj){
    if(typeof array.indexOf == 'function'){
        return array.indexOf(obj);
    }
    for (i = 0; i <array.length; i++) {
        if(array[i] == obj){
            return i;
        }
    }
    return -1;
}


console.log(createArrayCity(dataItaly, 'Lombardia'))

Demo: Fiddle

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.