Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用下划线.js对数组排序时遇到问题_Javascript - Fatal编程技术网

Javascript 使用下划线.js对数组排序时遇到问题

Javascript 使用下划线.js对数组排序时遇到问题,javascript,Javascript,我正在尝试对下面的列表进行排序。但我明白了: B 布勒 贝勒 A 艾格尼斯 C 卡拉 我希望: A 艾格尼斯 B 布勒 贝勒 C 卡拉 我在这里看到: 任何帮助都会很好 var series = [ { slug: "et", title: "Buller" }, { slug: "fem", title: "Bøller" }, { slug: "to", title: "Agnes" }, { slug: "tre"

我正在尝试对下面的列表进行排序。但我明白了:

B 布勒 贝勒

A 艾格尼斯

C 卡拉

我希望:

A 艾格尼斯

B 布勒 贝勒

C 卡拉

我在这里看到:

任何帮助都会很好

var series =
    [
        { slug: "et", title: "Buller" },
         { slug: "fem", title: "Bøller" },
        { slug: "to", title: "Agnes" },
        { slug: "tre", title: "Cara" }
    ];  




   function sortByName (array){
    return _.sortBy(array, "title");
}





groupedContacts = _.groupBy(series, function(contact){ 
    return contact.title.substr(0,1); 
}); 


   _.each(groupedContacts, function (series, key) {



    console.log(key); // writes the Index letter

    // optional sort

 var sortedContacts = _.sortBy(series, function (contact) {
         return contact.title;
     });


     _.each(sortedContacts, function(contact) {
         // Writes the contact name
         console.log(contact.title); 

     });
});    

如果要进行排序,为什么需要进行分组。删除以下代码

groupedContacts = _.groupBy(series, function(contact){ 
    return contact.title.substr(0,1); 
}); 


_.each(groupedContacts, function (series, key) {


    console.log(key); // writes the Index letter

    // optional sort

    var sortedContacts = _.sortBy(series, function (contact) {
         return contact.title;
    });


    _.each(sortedContacts, function(contact) {
        // Writes the contact name
        console.log(contact.title); 

    });
});
然后用sortBy

变量系列=[ {slug:et,title:Buller}, {slug:fem,title:Bøller}, {slug:to,title:Agnes}, {slug:tre,title:Cara} ]; var sorted=uu.sortByseries,“title”; document.getElementById'sorted'。innerHTML=JSON.stringifysorted;