解析数据并按字母顺序按字母顺序 [英] Parse data and order alphabetically under letter

查看:103
本文介绍了解析数据并按字母顺序按字母顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我想要成为的:

这是我的javascript:

This is my javascript:

var retrievedObject = localStorage.getItem('exhibitor');

    // CALL FUNCTION
    parsePerObject(JSON.parse(retrievedObject));

    function parsePerObject(data){

    }

这是我在localStorage中的对象:

This is my object in my localStorage :

{"41873":{"id":"41873","external_id":","eventid":"5588","venueid":"0","exhibitorcategoryid":"0",名称:"尼尔斯 Vroman," shortname:"," booth:"," imageurl:"," mapid:" 0," y1:" 0," x1:" 0," x2 :" 0," y2:" 0," description:"尼尔斯 uit Zulte.,"电话:" 0497841121,"地址:" Drogenboomstraat 54,"电子邮件:" vroman.niels@hotmail.com,"网络:" http://nielsvroman.be ," code:","用户名:","密码:"," image1:"," imagedescription1:"," image2:"," imagedescription2":","image3":","imagedescription3":","image4":","imagedescription4":","image5":","imagedescription5":","image6": ","imagedescription6":","image7":","imagedescription7":","image8":","imagedescription8":","image9":","imagedescription9":"," image10:"," imagedescription10:"," image11:"," imagedescription11:"," image12:"," imagedescription12:"," image13:"","imagedescription13":","image14":","imagedescription14":","image15":","imagedescription15":","image16":","imagedescription16":", "image17":","imagedescription17":","image18":","imagedescription18":","image19":","imagedescription19":","image20":","imagedescription20:","订单:" 0,"品牌:[],"类别:[]," linktodetails:true," imagethumb:"}," 41877:{" id":"41877",外部_id:"," eventid:" 5588," venueid:" 0," exhibitorcategoryid:" 0," name:" Ferdau Daems," shortname:"," booth:"," imageurl:"," mapid:" 0," y1:" 0," x1:" 0," x2 :" 0," y2:" 0,"描述:" Ferdau Daems","tel":"0497683697","address":"Waregem","email":"fer.dau@gmail.com","web":"

{"41873":{"id":"41873","external_id":"","eventid":"5588","venueid":"0","exhibitorcategoryid":"0","name":"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":"","image2":"","imagedescription2":"","image3":"","imagedescription3":"","image4":"","imagedescription4":"","image5":"","imagedescription5":"","image6":"","imagedescription6":"","image7":"","imagedescription7":"","image8":"","imagedescription8":"","image9":"","imagedescription9":"","image10":"","imagedescription10":"","image11":"","imagedescription11":"","image12":"","imagedescription12":"","image13":"","imagedescription13":"","image14":"","imagedescription14":"","image15":"","imagedescription15":"","image16":"","imagedescription16":"","image17":"","imagedescription17":"","image18":"","imagedescription18":"","image19":"","imagedescription19":"","image20":"","imagedescription20":"","order":"0","brands":[],"categories":[],"linktodetails":true,"imagethumb":""},"41877":{"id":"41877","external_id":"","eventid":"5588","venueid":"0","exhibitorcategoryid":"0","name":"Ferdau Daems","shortname":"","booth":"","imageurl":"","mapid":"0","y1":"0","x1":"0","x2":"0","y2":"0","description":"Ferdau Daems","tel":"0497683697","address":"Waregem","email":"fer.dau@gmail.com","web":"http://ferdau.be","code":"","username":"","password":"","image1":"","imagedescription1":"","image2":"","imagedescription2":"","image3":"","imagedescription3":"","image4":"","imagedescription4":"","image5":"","imagedescription5":"","image6":"","imagedescription6":"","image7":"","imagedescription7":"","image8":"","imagedescription8":"","image9":"","imagedescription9":"","image10":"","imagedescription10":"","image11":"","imagedescription11":"","image12":"","imagedescription12":"","image13":"","imagedescription13":"","image14":"","imagedescription14":"","image15":"","imagedescription15":"","image16":"","imagedescription16":"","image17":"","imagedescription17":"","image18":"","imagedescription18":"","image19":"","imagedescription19":"","image20":"","imagedescription20":"","order":"0","brands":[],"categories":[],"linktodetails":true}}

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

Does anyone know how I can sort alphabetically on the name and make a header from the first letter?

推荐答案

假设您要使用对象的Array而不是对象的Object来进行索引和排序.对象没有顺序.

Say you have an Array of objects, not an Object of objects, to enable indexing and sorting. Objects don't have order.

您从localStorage检索到它.您解析它.

You retrieve it from localStorage. You parse it.

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数组,获取唯一的起始字母,将它们排列成一个数组,然后根据需要显示数据.它应该相当简单.

You can run through the people array, get the unique start letters, make an array of out them, and then display data as you want. It should be fairly simple.

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
     };
};

在这一点上,您有一个像这样的对象:

At this point you have an object like this:

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

只需使用以上数据即可创建显示.还有什么,我必须给您开票:).

Just use the above data to create the display. Anything more and I would have to invoice you:).

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

The tricks here are Array.prototype.sort(here is more on it) and String.prototype.localeCompare(read more here).

这篇关于解析数据并按字母顺序按字母顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆