Javascript 分析数据并按字母顺序排列
这就是我想要成为的: 这是我的javascript:Javascript 分析数据并按字母顺序排列,javascript,jquery,arrays,object,Javascript,Jquery,Arrays,Object,这就是我想要成为的: 这是我的javascript: var retrievedObject = localStorage.getItem('exhibitor'); // CALL FUNCTION parsePerObject(JSON.parse(retrievedObject)); function parsePerObject(data){ } 这是我在本地存储中的对象: {“41873”:{“id”:“41873”,“外部id”:“event
var retrievedObject = localStorage.getItem('exhibitor');
// CALL FUNCTION
parsePerObject(JSON.parse(retrievedObject));
function parsePerObject(data){
}
这是我在本地存储中的对象:
{“41873”:{“id”:“41873”,“外部id”:“eventid”:“5588”,“venueid”:“0”,“参展商类别id”:“0”,“名称”:“Niels”
Vroman、shortname、booth、mapid、0、y1、x1、0、x2、0、y2、0、描述、Niels
uit Zulte.,“电话”:“0497841121”,“地址”:“Drogenboomstraat”
54、“电子邮件”:“vroman。niels@hotmail.com“,”web“:”代码“:”用户名“:”密码“:”image1“:”imagedescription1“:”image2“:”image3“:”imagedescription3“:”image4“:”imagedescription4“:”image5“:”image6“:”imagedescription6“:”image7“:”imagedescription7“:”image8“,”,“imagedescription8:”“image9:”“imagedescription9:”“image10:”“imagedescription10:”“image11:”“imagedescription11:”“image12:”“imagedescription12:”“image13:”“imagedescription13:”“image14:”“imagedescription14:”“image15:”“imagedescription15:”“image16:”“imagedescription16:”“imagedescription16:”“image17:”“imagedescription17”“:”,“image18:“,”imagedescription18:“,”image19:“,”imagedescription19:“,”image20:“,”订单“:”0”,“品牌“:[”,”类别“:[”,”链接详情“:”true,“imagethumb:”,”41877:“{”id:“41877”,“外部id:”eventid:“5588”,“venueid:“0”,“参展商类别id:”0”,“名称“:”Ferdau”
Daems、“shortname”:“booth”:“imageurl”:“mapid”:“0”、“y1”:“0”、“x1”:“0”、“x2”:“0”、“y2”:“0”、“描述”:“Ferdau”
Daems,“电话”:“0497683697”,“地址”:“Waregem”,“电子邮件”:“fer。dau@gmail.com“,”web“:”代码“:”用户名“:”密码“:”image1“:”imagedescription1“:”image2“:”image3“:”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:“,”订单“:”0”,“品牌“:[],”类别“:[],”链接详细信息“:true}}”
有人知道我如何按名称的字母顺序排序,并从第一个字母开始创建标题吗?假设您有一个对象的
数组,而不是对象的对象来启用索引和排序。对象没有顺序
您可以从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.
您可以运行人员数组,获取唯一的起始字母,将它们组成一个数组,然后根据需要显示数据
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
};
};
只需使用上述数据创建显示。如果需要更多信息,我将向您开具发票:)
这里的技巧是Array.prototype.sort
()和String.prototype.localeCompare
()。对象没有顺序。应该使用数组。
a: [person1, person2, person5, etc..]//the people at A.
b: [person 3, person 4, etc..]// the people at B.