使用函数中的变量对对象数组中的值进行排序

我正在尝试创建一个函数,它接受对值进行排序所需的对象的键(在本例中为“kills”)。我尝试使用按字符串属性值对对象数组进行排序dynamicSort中所述,但我只是得到返回的列表。关于我在这里做错了什么有什么想法吗?

const list = [

    {

        name: 'compass',

        kills: 35,

        assists: 312

    },

    {

        name: 'another one',

        kills: 52,

        assists: 32

    },

    {

        name: 'another anothe one',

        kills: 12,

        assists: 30

    }

];


const sortByType = (property) => {

  return function (a, b) {

    let result;

    if (a[property] < b[property]) {

      result = -1;

    }

    if (a[property] > b[property]) {

      result = 1;

    }

    else {

      result = 0;

    }

    return result;

  };

}


let newList = list.sort(sortByType('kills'));

console.log(newList);



阿波罗的战车
浏览 110回答 2
2回答

拉风的咖菲猫

嘿,您可以使用排序函数中的属性简单地对数组进行排序。升序a[property] - b[property]&nbsp;降序b[property] - a[property]按名称排序是将所有名称转换为一个数组,对它们进行排序,然后循环进行排序。这运行起来n^2所以值得研究优化,但这会让你越过终点线。name注意:此功能将不起作用undefined。const list = [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; name: 'compass',&nbsp; &nbsp; &nbsp; &nbsp; kills: 35,&nbsp; &nbsp; &nbsp; &nbsp; assists: 312&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; name: 'another one',&nbsp; &nbsp; &nbsp; &nbsp; kills: 52,&nbsp; &nbsp; &nbsp; &nbsp; assists: 32&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; name: 'another anothe one',&nbsp; &nbsp; &nbsp; &nbsp; kills: 12,&nbsp; &nbsp; &nbsp; &nbsp; assists: 30&nbsp; &nbsp; }]const sortByString = () => {&nbsp; &nbsp; const strings = list.map(prop => prop.name).sort();&nbsp; &nbsp; let sorted = [];&nbsp; &nbsp; for(const idx in strings){&nbsp; &nbsp; &nbsp; &nbsp; let currString = strings[idx];&nbsp; &nbsp; &nbsp; &nbsp; for(const objIdx in list){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(currString, list[objIdx].name)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(list[objIdx].name === currString){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sorted.push(list[objIdx]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return sorted;}const dynamicSortByType = (property) => {&nbsp; &nbsp; return typeof list[0][property] === 'string' ?&nbsp;&nbsp; &nbsp; sortByString() : list.sort((a,b) => a[property] - b[property])}console.log(dynamicSortByType('name'))

天涯尽头无女友

简短回答:你只是漏掉了这个词else。长答案:你有一个看起来像这样的块if () {}if () {}else {}应该是这样的:if () {}else if () {}else {}请注意else在第二个之前添加了if。如果没有它,第二个 if-else 将运行,最后的 else 将取代第一个 的 true 情况所做的设置if。例如,任何时候if (a[property] < b[property]) {,它实际上都会落入秒数if并else导致设置result = 0。这是您进行了微小修复的片段:const list = [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; name: 'compass',&nbsp; &nbsp; &nbsp; &nbsp; kills: 35,&nbsp; &nbsp; &nbsp; &nbsp; assists: 312&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; name: 'another one',&nbsp; &nbsp; &nbsp; &nbsp; kills: 52,&nbsp; &nbsp; &nbsp; &nbsp; assists: 32&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; name: 'another anothe one',&nbsp; &nbsp; &nbsp; &nbsp; kills: 12,&nbsp; &nbsp; &nbsp; &nbsp; assists: 30&nbsp; &nbsp; }];const sortByType = (property) => {&nbsp; return function (a, b) {&nbsp; &nbsp; let result;&nbsp; &nbsp; if (a[property] < b[property]) {&nbsp; &nbsp; &nbsp; result = -1;&nbsp; &nbsp; }&nbsp; &nbsp; else if (a[property] > b[property]) {&nbsp; /* FIXED:&nbsp; added `else` on this line */&nbsp; &nbsp; &nbsp; result = 1;&nbsp; &nbsp; }&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; result = 0;&nbsp; &nbsp; }&nbsp; &nbsp; return result;&nbsp; };}let newList = list.sort(sortByType('kills'));console.log(newList);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript