如何在Javascript对象中选择具有最低“计数”的值

时间:2018-11-13 21:38:02

标签: javascript lodash

通过使用lodash的_.countBy函数,我生成了一个计数对象,计算一个值在数组中出现的次数。

在“值->计数”对象中选择最低计数的最佳方法是什么?

 var/input = ["A","A","B","B","C"];
 var/tally = _.countBy(input); // Tally is {"A":2, "B":2, "C":1}

 // [...solution...]

 Console.assert(lowest == "C");

不需要绑带特殊处理,只要绑带数量最少即可。 Lodash可用。

6 个答案:

答案 0 :(得分:3)

您可以减少键的数组,并通过检查该值来获得最小的键。

var tally = { A: 2, B: 2, C: 1 },
    lowest = Object.keys(tally).reduce((a, b) => tally[a] < tally[b] ? a : b);

console.log(lowest);

一种lodash方法,将属性配对并在索引1处获得最小值,并获取数组的第一个元素。

var input = ["A", "A", "B", "B", "C"],
    lowest = _(input)
        .countBy()                    // { A: 2, B: 2, C: 1 }
        .toPairs()                    // [["A", 2], ["B", 2], ["C", 1]]
        .minBy(1)                     // ["C", 1]
        [0];

console.log(lowest);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

答案 1 :(得分:0)

这可以通过以下方式实现:

var tally = {"A":2, "B":2, "C":1};

var tallyItemSmallestKey = Object
.entries(tally)
.sort((entry0, entry1) => entry0[1] - entry1[1] < 0 ? -1 : 1)
.map(entry => entry[0])[0];

console.log( tallyItemSmallestKey );

答案 2 :(得分:0)

如果对输入数组进行排序,则可以简化为非_解决方案:

const input = ["A","A","B","B","C"].sort();

let curr = { count: 0 }, result = { count: 0 };
for(const value of input) {
  if(value === curr.value) curr.count++;
  else curr = { value, count: 1 };

  if(curr.count > result.count) result = curr;
}

console.log(result.value);

答案 3 :(得分:0)

您可以获取记录,然后检查最小值

const input = ["A", "A", "B", "B", "C"]

// get counts
const tally = input.reduce((obj, char) => {
  if(!obj[char])
   obj[char]=1
  else
   obj[char]++
  return obj
}, {})

// get min
let char = Reflect.ownKeys(tally).reduce((keep,current)=>{
  if(!keep) keep = current
  if(tally[current] < tally[keep])
     keep = current
  return keep
}, '')

console.log(char)
console.log(`'${char}' == 'A'`,char == 'A')
console.log(`'${char}' == 'C'`,char == 'C')

答案 4 :(得分:0)

使用lodash可以将countByentriesminBy用于类似这样的事情:

const input = ["A", "A", "B", "B", "C"]

const result = _.minBy(_.entries(_.countBy(input)), 1)
console.log(_.head(result))  // or just result[0]
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

或通过lodash链接:

const input = ["A", "A", "B", "B", "C"]

const result = _(input).countBy().entries().minBy(1)

console.log(_.head(result)) // or just result[0]
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

您还可以使用带有Array.reduceArray.sortObject.entries的ES6:

const input = ["A", "A", "B", "B", "C"]

const countBy = arr => arr.reduce((r,c) => (r[c] = (r[c] || 0) + 1, r), {})
const min = arr => arr.sort((a,b) => a[1] - b[1])[0] // sort asc and take first
console.log(min(Object.entries(countBy(input)))[0])

答案 5 :(得分:0)

如果您不想导入lodash提供的所有内容,则不能使用链接。这是_.flow()的基于Nina's answer的版本:

9
const { flow, countBy, entries, partialRight, minBy, last, head } = _;

const input = ["A", "A", "B", "B", "C"];

const getByLowestCount = flow([
  countBy,
  entries,
  partialRight(minBy, last),
  head
]);

const result = getByLowestCount(input);

console.log(result);