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

时间:2013-04-24 09:37:40

标签: javascript jquery arrays object

这就是我想成为的:

enter image description here

这是我的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”,“名称“:”尼尔斯   Vroman “ ”短名“: ”“, ”亭“: ”“, ”IMAGEURL“: ”“, ”的azazaz“: ”0“, ”Y1“: ”0“, ”X1“: ”0“,” X2 “:” 0" , “Y2”: “0”, “描述”:“尼尔斯   uit Zulte。“,”tel“:”0497841121“,”地址“:”Drogenboomstraat   54" , “电子邮件”: “vroman.niels@hotmail.com”, “网络”: “http://nielsvroman.be”, “代码”: “”, “用户名”: “”, “密码”: “”, “此搜索”: “”, “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”:真 “imagethumb”: “”}, “41877”:{ “ID”: “41877”, “EXTERNAL_ID”: “”, “事件ID”: “5588”, “venueid”:“0 “ ”exhibitorcategoryid“: ”0“, ”名“:” Ferda ü   Daems “ ”短名“: ”“, ”亭“: ”“, ”IMAGEURL“: ”“, ”的azazaz“: ”0“, ”Y1“: ”0“, ”X1“: ”0“,” X2 “:” 0" , “Y2”: “0”, “描述”:“Ferdau   Daems “ ”电话“: ”0497683697“, ”地址“: ”瓦勒海姆“, ”电子邮件“: ”fer.dau@gmail.com“, ”网络“: ”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”:真}}

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

1 个答案:

答案 0 :(得分:2)

假设您有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.sorthere is more on it)和String.prototype.localeCompareread more here)。