Si tous les noms peuvent être garantis d'être unique, vous pouvez faire quelque chose comme ceci:
var data_array = [
{"name":"aaa","0":"aaa","city":"paris","1":"paris","school":"gtdzh","2":"gtdzh"},
{"name":"bbb","0":"bbb","city":"berlin","1":"berlin","school":"gdezh","2":"gdezh"},
{"name":"ccc","0":"ccc","city":"new york","1":"new york","school":"asdzh","2":"asdzh"},
{"name":"aaa","0":"aaa","city":"sidney","1":"sidney","school":"gtdcv","2":"gtdcv"},
{"name":"bbb","0":"bbb","city":"paris","1":"paris","school":"gtdzh","2":"gtdzh"}
];
var l = data_array.length;
var i = 0
// We will use dict to store our output
var dict = {};
// Loop over the entire array
while (i < l) {
// Grab references to everything we need
var name = data_array[i].name;
var city = data_array[i].city;
// If we haven't seen this person before, add them to the dict
if (! Object.prototype.hasOwnProperty.call(dict, name)) {
dict[name] = {};
}
// Similarly, if we haven't heard of them studying in this city yet
// add that city to the "dictionary" of cities they've studied in
// and set the count of times they have studied in that city to 1.
if (! Object.prototype.hasOwnProperty.call(dict[name], city)) {
dict[name][city] = 1;
// Otherwise, increment the number of times they have studied in that city.
} else {
dict[name][city] += 1;
}
i++;
}
Le le résultat final ressemblera à quelque chose comme ceci:
dict = {
"aaa": {
"paris": 1,
"sidney": 1
},
"bbb": {
"berlin": 1,
"paris": 1
},
"ccc": {
"new york": 1
}
};
Bien sûr, il y a de meilleures façons d'y parvenir si vous faites ce genre de chose à plusieurs reprises - tout de changer la façon dont les données sont envoyées du serveur à la construction ou à l'aide de bibliothèques auxiliaires comme Underscore pour faire ce genre de données-munging. Il y a aussi quelques implémentations de bases de données Javascript, mais je n'ai travaillé avec aucune d'entre elles donc je ne peux rien recommander à cet égard.
http://stackoverflow.com/questions/1946165/json-find-in-javascript –