在字母下按字母顺序解析数据和顺序

这就是我想成为的:

在此处输入图像描述

这是我的javascript:

var retrievedObject = localStorage.getItem('exhibitor'); // CALL FUNCTION parsePerObject(JSON.parse(retrievedObject)); function parsePerObject(data){ } 

这是我在localStorage中的对象:

{ “41873”:{ “ID”: “41873”, “EXTERNAL_ID”: “”, “事件ID”: “5588”, “venueid”: “0”, “exhibitorcategoryid”: “0”, “名”:” Niels Vroman“,”shortname“:”“,”booth“:”“,”imageurl“:”“,”mapid“:”0“,”y1“:”0“,”x1“:”0“,” x2“:”0“,”y2“:”0“,”description“:”Niels uit Zulte。“,”tel“:”0497841121“,”address“:”Drogenboomstraat 54“,”email“:”vroman。 niels@hotmail.com“,”web“:” http://nielsvroman.be “,”code“:”“,”username“:”“,”password“:”“,”image1“:”“,” imagedescription1 “:” “ ”图像2“: ”“, ”imagedescription2“: ”“, ”图像3“: ”“, ”imagedescription3“: ”“, ”图像4“: ”“, ”imagedescription4“: ”“,” 图像5 “:””, “imagedescription5”: “”, “image6”: “”, “imagedescription6”: “”, “image7”: “”, “imagedescription7”: “”, “image8”: “”, “imagedescription8” : “”, “图像9”: “”, “imagedescription9”: “”, “image10”: “”, “imagedescription10”: “”, “图像11”: “”, “imagedescription11”: “”, “image12”: “”, “imagedescription12”: “”, “image13”: “”, “imagedescription13”: “”, “image14”: “”, “imagedescription14”: “”, “image15”: “”, “imagedescription15”:” “ ”image16“: ”“, ”imagedescription16“: ”“, ”image17“: ”“,” imagedescription 17 “:” “ ”image18“: ”“, ”imagedescription18“: ”“, ”image19“: ”“, ”imagedescription19“: ”“, ”image20“: ”“, ”imagedescription20“: ”“,” 顺序“:” 0″ , “品牌”:[], “类别”:[], “linktodetails”:真 “imagethumb”: “”}, “41877”:{ “ID”: “41877”, “EXTERNAL_ID” :“”,“eventid”:“5588”,“venueid”:“0”,“Exhibitorcategoryid”:“0”,“name”:“Ferdau Daems”,“shortname”:“”,“booth”:“” “IMAGEURL”: “”, “的azazaz”: “0”, “Y1”: “0”, “X1”: “0”, “×2”: “0”, “Y2”: “0”,“描述“:”Ferdau Daems“,”tel“:”0497683697“,”地址“:”Waregem“,”email“:”fer.dau@gmail.com“,”web“:” http://ferdau.be “ “代码”: “”, “用户名”: “”, “密码”: “”, “图像1”: “”, “imagedescription1”: “”, “图像2”: “”, “imagedescription2”: “”, “图像3”: “”, “imagedescription3”: “”, “图像4”: “”, “imagedescription4”: “”, “图像5”: “”, “imagedescription5”: “”, “image6”: “”,” imagedescription6 “:” “ ”image7“: ”“, ”imagedescription7“: ”“, ”image8“: ”“, ”imagedescription8“: ”“, ”图像9“: ”“, ”imagedescription9“: ”“,” image10 “:” “ ”imagedescription10“: ”“, ”图像11“: ”“, ”imagedescription11“: ”“, ”image12“: ”“, ”imagedescription12“: ”“, ”image13“:” ”, “imagedescription13”: “”, “image14”: “”, “imagedescription14”: “”, “image15”: “”, “imagedescription15”: “”, “image16”: “”, “imagedescription16”: “” “image17”: “”, “imagedescription17”: “”, “image18”: “”, “imagedescription18”: “”, “image19”: “”, “imagedescription19”: “”, “image20”: “”, “imagedescription20”: “”, “订单”: “0”, “品牌”:[], “类别”:[], “linktodetails”:真}}

有谁知道我如何按字母顺序排序名称并从第一个字母开始标题?

假设您有一个对象Array ,而不是Object的对象,以启用索引和排序。 对象没有订单。

您从localStorage检索它。 你解析它。

 var people = JSON.parse(localStoarge.getItem("exhibitor"); // now you have an array of objects, each object representing a person. // regardless of what structure you have now, change it to achieve this. var comparePersons = function(a, b) { // this function will compare two people objects. return a.name.localeCompare(b.name); // it's using String.prototype.localeCompare which returns 1 if a.name > b.name, // 0 for equal and -1 for smaller. localeCompare is lexicographic comparison. }; people.sort(comparePersons); // now you have the people sorted alphabetically. 

您可以遍历people数组,获取唯一的开始字母,创建一个数组,然后根据需要显示数据。 它应该相当简单。

 var letters = '', groups = {}; for (var i = 0, len = people.length; i < len; i++) { var letterKey = people[i].name.charAt(0).toLowerCase();// get the first letter if (letters.indexOf(letterKey)) == -1) { letters += letterKey; groups[letterKey] = [people[i]];// index the people by unique letters. } else { groups[letterKey].push([people[i]]);// add to the existing list. Another Syntax fix }; }; 

此时你有一个像这样的对象:

 a: [person1, person2, person5, etc..]//the people at A. b: [person 3, person 4, etc..]// the people at B. 

只需使用上述数据即可创建显示。 还有更多,我必须给你发票:)。

这里的技巧是Array.prototype.sort ( 这里有更多 )和String.prototype.localeCompare ( 在这里阅读更多 )。