【问题标题】:Sorted values in Object Array using a variable in function使用函数中的变量对对象数组中的值进行排序
【发布时间】:2020-11-02 18:37:39
【问题描述】:

我正在尝试创建一个函数,它接收对值进行排序所需的对象的键(在本例中为“kills”)。我尝试使用Sort array of objects by string property value 中所述的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);

已编辑:更新了代码的使用方式。

【问题讨论】:

  • 展示你是如何使用它的。
  • 请发帖minimal reproducible example。您可以使用Stack Snippet 使其可执行。
  • 请注意,您的方法的全部内容可以简化为return a[property] - b[property]
  • @Taplar 这仅适用于数字数据,不适用于字符串。
  • 当然可以,但考虑到他们目前的方法,它会是一样的。

标签: javascript sorting object


【解决方案1】:

您可以使用 sort 函数中的属性对数组进行简单的排序。

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

按名称排序是将所有名称转换为对它们进行排序的数组,然后循环进行排序。这在n^2 中运行,因此值得研究优化,但这会让你越过终点线。

注意:此功能将不起作用是nameundefined

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'))

【讨论】:

  • 问题中的方法应该可以按字符串排序。
  • 所以我添加了字符串排序。它在 n^2 中运行,因此对于非常大的对象,它可能会很慢。值得研究优化。将字符串放入数组进行排序是我能想到的最佳选择
【解决方案2】:

简答:

你只是错过了else这个词。

长答案:

你有一个看起来像这样的块

if () {
}
if () {
}
else {
}

应该是这样的:

if () {
}
else if () {
}
else {
}

注意在第二个if 之前添加else。没有它,第二个 if-else 将运行,最后一个 else 将取代第一个 if 的真实案例所做的设置。

例如,任何时候if (a[property] &lt; b[property]) {,它实际上会通过第二个ifelse 并导致设置result = 0

这是你的 sn-p 做了微小的修复:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多