【问题标题】:Traverse an array and get lowest 2 entries for a given name遍历数组并获取给定名称的最低 2 个条目
【发布时间】:2016-11-15 15:37:14
【问题描述】:

我有一个带有键“名称”和“点”的数组。一个特定名称可以有多个条目。什么是形成具有名称、点和基于点的新键类型的新数组的理想方法。 例子: 输入:

[{"name":"A", "points": 9},
{"name":"A", "points": 5},
{"name":"A", "points": 4},
{"name":"A", "points": 1},
{"name":"A", "points": 3},
{"name":"A", "points": 6},
{"name":"B", "points": 5},
{"name":"B", "points": 1},
{"name":"B", "points": 2},
{"name":"B", "points": 3}]

Lowest points - typeA
Second lowest points - typeB

输出:

[{"name":"A", "points": 1, "type":"typeA"},
{"name":"A", "points": 3, "type":"typeB"},
{"name":"B", "points": 1, "type":"typeA"},
{"name":"B", "points": 2, "type":"typeB"}]

【问题讨论】:

  • 如果您想要的输出与输入的行数不同,lodash 对您没有多大帮助。标准的循环将是您的朋友。
  • @libik 我已经编辑了使用正常遍历的问题,这应该是最佳的,因为列表可能大约 1000-2000 长

标签: arrays node.js lodash


【解决方案1】:

我认为这将是一个很好的解决方案:

const input = [{"name":"A", "points": 9},
                {"name":"A", "points": 5},
                {"name":"A", "points": 4},
                {"name":"A", "points": 1},
                {"name":"A", "points": 3},
                {"name":"A", "points": 6},
                {"name":"B", "points": 5},
                {"name":"B", "points": 1},
                {"name":"B", "points": 2},
                {"name":"B", "points": 3}];

//this function will be passed to Array's "reduce" function, and will return the item with the lowest points
const reduceLowest = (lowest, current) => current.points < lowest.points ? current : lowest;

//this function receives an item and returns a new item with an additional "type" property
const addType = (item, typeToAdd) => { return { name : item.name, points : item.points, type : typeToAdd } };

//this function receives an array and returns another array with its two lowests items (typeA and typeB)
const returnTypeATypeB = (arr) => {

    var lowestTypeA = arr       
        .reduce(reduceLowest) //gets the lowest item
        ;

    var lowestTypeB = arr
        .filter((item) => item != lowestTypeA) //removes the item that already is the lowest from the array     
        .reduce(reduceLowest) //gets the lowest item
        ;

    //return an array containing the typeA and typeB items
    return [ addType(lowestTypeA, "typeA"), addType(lowestTypeB, "typeB")];

};          

const output = 
    returnTypeATypeB(
        input.filter((item) => item.name === "A")
    ) //gets typeA and typeB from the items which contains the name "A"
    .concat( //concat the previous list with..
        returnTypeATypeB(
            input.filter((item) => item.name === "B") 
        ) //typeA and typeB from the items which contains the name "B"
    );

console.log(output);
//prints..
// [ { name: 'A', points: 1, type: 'typeA' },
//  { name: 'A', points: 3, type: 'typeB' },
//  { name: 'B', points: 1, type: 'typeA' },
//  { name: 'B', points: 2, type: 'typeB' } ]

【讨论】:

  • @dearchid 感谢您的想法。我实际上是通过基于名称然后按点进行排序来完成的。然后切片每个名称的前 2 个并相应地添加类型。不得不概括它,因为列表很大,而不仅仅是 A n B。
  • 是否有基于 2 键的对象数组自然排序的 npm 模块
猜你喜欢
  • 1970-01-01
  • 2017-05-19
  • 2012-09-14
  • 1970-01-01
  • 2018-05-16
  • 1970-01-01
  • 2020-10-31
  • 1970-01-01
  • 2012-06-25
相关资源
最近更新 更多