tags:

views:

69

answers:

2

hallo Thank you for your answers. I tried 2 codes but it does not work.i have a JSON object look like this

[{"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"}]

i want to fill a drop down list with distinct values selected from a field of my Json object

for Example: I have 2 persons who had study in 2 cities aaa and bbb. how to do so that i does not have a duplication into my drop down list.

+1  A: 

jQuery (& Javascript) don't have much for dealing for arrays of data. I would suggest that you look into the Underscore js library (which is designed to complement jQuery) which handles all sorts of array manipulations.

James Curran
+1  A: 

You could iterate through the object; creating a new object only with unique fields (something along these lines would work):

function array_search (needle, haystack) {
    var key = '';
    for (key in haystack) {        
        if ((haystack[key] === needle) || (haystack[key] == needle)) {
            return key;
        }
    }
     return false;
}

(the function above c/o http://phpjs.org/functions/array_search:335)

var myNames = new Array();
var myFilteredObject = new Array();
for (i in myObject){
    if (array_search(myObject[i].name, myNames) == false){
        var myNames[i] = myObject[i].name;
        var myFilteredObject[] = myObject[i];
    }  
}
akellehe