【问题标题】:Sort Array of Objects by value of unknown keys按未知键的值对对象数组进行排序
【发布时间】:2021-10-05 19:07:57
【问题描述】:

我正在尝试按对象的值对对象数组进行排序,并按降序返回具有最高值的 3 个键。数组是:

let obj = [
{'tom':4},
{'bill':5},
{'tina':6},
{'tim': 3}]

我正在寻找的解决方案:['tina', 'bill', 'tom']

如果有三个以上的值满足我也想列出的条件,例如:

let obj = [
{'tom':4},
{'bill':5},
{'tina':6},
{'tim': 3},
{'jim':4]

我正在寻找的解决方案:['tina', 'bill', 'tom', 'jim']

我试图做类似的事情:

Object.entries(obj).sort((a,b) => b[1] - a[1]).map(value => ({[value[0]]: value[1]}))

但找不到正确的方法。

【问题讨论】:

  • 每个对象总是只有一个键吗?
  • 是的,每个对象总是只有 1 个键

标签: javascript arrays sorting object


【解决方案1】:

let obj = [{
    'tom': 4
  },
  {
    'bill': 5
  },
  {
    'tina': 6
  },
  {
    'tim': 3
  },
  {
    'jim': 4
  }
]

let result = obj.map(object => {
  for (name in object) {
    return [name, object[name]]
  }
}).sort((a, b) => b[1] - a[1]).map(object => object[0])

console.log(result)

【讨论】:

    【解决方案2】:
    1. 首先按对象的值对对象数组进行排序(使用.sortObject.values。)
    2. 选择前 3 项的值,(使用 .slice.mapObject.values。)
    3. 从原始数组中选择所有值在前 3 个数组中的项目,(使用.reduce

    您最终应该得到一组具有前 3 个最高值的键。

    如下所示:

    let obj = [
        {
            tom: 4,
        },
        {
            bill: 5,
        },
        {
            tina: 6,
        },
        {
            tim: 3,
        },
        {
            jim: 4,
        },
    ];
    
    // An array of values of the Top 3 sorted objects
    let top3 = obj
        .map((obj) => Object.values(obj)[0])
        .sort((a, b) => b - a)
        .slice(0, 3);
        
    
    // An array the Top Keys that are found in t3
    let topKeys = obj.reduce((acc, curr) => {
        if (top3.includes(Object.values(curr)[0])) acc.push(Object.keys(curr)[0]);
        return acc;
    }, []);
    
    console.log(topKeys);

    【讨论】:

      【解决方案3】:

      第 1 步。

      使用Array#reduce创建一个看起来像{[value: number]: Array<string>}的对象,由于默认的对象键排序,它将自动排序。

      第 2 步。

      使用Object.values 获取{[value: number]: Array<string>} 对象的值列表。

      第 3 步。

      使用 Array#slice 获取最后 3 项。

      第 4 步。

      使用Array#flat 将数组从Array<Array<string>> 转换为Array<string>

      let obj = [
        { 'tom': 4 },
        { 'bill': 5 },
        { 'tina': 6 },
        { 'tim': 3 },
        { 'jim': 4 }
      ];
      
      var sortedObj = obj.reduce(function(result, item) {
        // get the name and value of the current object
        var key = Object.keys(item)[0], value = item[key];
        // get the list of names for that value
        var list = result[value];
        // if this is the first occurence of the value, create the array
        if (!list) result[value] = list = [];
        // add the name to the array
        list.push(key);
        return result;
      }, {});
      
      var sortedList = Object.values(sortedObj);
      
      var last3 = sortedList.slice(sortedList.length - 3);
      
      var result = last3.flat();
      
      console.log(result);

      【讨论】:

        【解决方案4】:

        你可以

        • map 对象的条目,
        • sort 基于第一个条目的值的二维条目数组
        • map 获取每个对象的第一个值

        const input = [{ tom: 4 }, { bill: 5 }, { tina: 6 }, { tim: 3 }];
        
        const output = input
                        .map(Object.entries)
                        .sort(([a], [b]) => b[1] - a[1])
                        .map(([[name]]) => name);
        
        console.log(output)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-07-11
          • 2021-10-30
          • 1970-01-01
          • 2021-06-06
          • 1970-01-01
          • 2016-12-23
          • 1970-01-01
          相关资源
          最近更新 更多