亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用函數中的變量對對象數組中的值進行排序

使用函數中的變量對對象數組中的值進行排序

阿波羅的戰車 2023-07-06 11:05:45
我正在嘗試創建一個函數,它接受對值進行排序所需的對象的鍵(在本例中為&ldquo;kills&rdquo;)。我嘗試使用按字符串屬性值對對象數組進行排序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);
查看完整描述

2 回答

?
拉風的咖菲貓

TA貢獻1995條經驗 獲得超2個贊

嘿,您可以使用排序函數中的屬性簡單地對數組進行排序。

升序a[property] - b[property] 降序b[property] - a[property]

按名稱排序是將所有名稱轉換為一個數組,對它們進行排序,然后循環進行排序。這運行起來n^2所以值得研究優化,但這會讓你越過終點線。

name注意:此功能將不起作用undefined。

const list = [

    {

        name: 'compass',

        kills: 35,

        assists: 312

    },

    {

        name: 'another one',

        kills: 52,

        assists: 32

    },

    {

        name: 'another anothe one',

        kills: 12,

        assists: 30

    }

]



const sortByString = () => {

    const strings = list.map(prop => prop.name).sort();

    let sorted = [];

    for(const idx in strings){

        let currString = strings[idx];

        for(const objIdx in list){

            console.log(currString, list[objIdx].name)

            if(list[objIdx].name === currString){

                sorted.push(list[objIdx]);

            }

        }

    }

    return sorted;

}

const dynamicSortByType = (property) => {

    return typeof list[0][property] === 'string' ? 

    sortByString() : list.sort((a,b) => a[property] - b[property])

}

console.log(dynamicSortByType('name'))


查看完整回答
反對 回復 2023-07-06
?
天涯盡頭無女友

TA貢獻1831條經驗 獲得超9個贊

簡短回答:

你只是漏掉了這個詞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 = [

    {

        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;

    }

    else if (a[property] > b[property]) {  /* FIXED:  added `else` on this line */

      result = 1;

    }

    else {

      result = 0;

    }

    return result;

  };

}


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

console.log(newList);


查看完整回答
反對 回復 2023-07-06
  • 2 回答
  • 0 關注
  • 169 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號