[ {"name": "D"}, {"name": "G"}, {"name": "A"}, {"name": "Z"}, {"name": "H"}, {"name": "K"}, {"name": "B"}, {"name": "C"}, ]具体比这复杂,每个对象里面除了name外,还包括字母对应品牌的车型。
这里返回的数据就如上面的一样,是乱序的,要进行排序,对字母进行比较,可以用到localeCompare方法,而排序,可以使用js的sort方法,那么就好办了,如下:
var brands = [ {"name": "D",}, {"name": "G",}, {"name": "A",}, {"name": "Z",}, {"name": "H",}, {"name": "K",}, {"name": "B",}, {"name": "C",}, ] brands.sort(function(a,b){ return a.name.localeCompare(b.name) })输入结果如下:
[ {"name": "A"}, {"name": "B"}, {"name": "C"}, {"name": "D"}, {"name": "G"}, {"name": "H"}, {"name": "K"}, {"name": "Z"}, ]