喂 谢谢您的回答。 我尝试了2个代码,但它不起作用。我有一个JSON对象看起来像这样
[{"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"}]
我想填充一个下拉列表,其中包含从我的Json对象
字段中选择的不同值例如: 我有2个人在2个城市aaa和bbb学习。如何做到我的下拉列表中没有重复。
答案 0 :(得分:1)
jQuery(& Javascript)没有太多用于处理数据数组。我建议您查看Underscore js library(用于补充jQuery),它可以处理各种数组操作。
答案 1 :(得分:1)
你可以遍历对象;仅使用唯一字段创建一个新对象(这些行中的某些内容可行):
function array_search (needle, haystack) {
var key = '';
for (key in haystack) {
if ((haystack[key] === needle) || (haystack[key] == needle)) {
return key;
}
}
return false;
}
(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];
}
}