在Javascript中有没有更有效的方法来获取对象中的多个最小值?
Is there a more efficient way to grab multiple minimum values in an object in Javascript?
我有一个解决方案,但我不确定这是否是最好的方法..我有一个看起来像这样的对象:
const obj = {
thingA: 1,
thingB: 2,
thingC: 1,
thingD: 1,
thingE: 4
}
我想获取所有具有最低值的键值对,然后能够随机 select 具有最低值的键值对之一(在本例中随机 select 三个键中的一个值为 1)。
这就是我所拥有的并且有效,但我想知道是否有一种 better/more 有效的方法:
const keyArray = Object.keys(obj)
const minArray = (keyArray) => {
const min = keyArray.reduce((prev, curr) => obj[curr] ? prev : curr);
const res = [];
for (const key of keyArray) {
if (obj[key] !== obj[min]) {
continue
}
res.push(key)
}
return res
}
const randomMinKey = minArray(keyArray)[Math.floor(Math.random()*minArray.length)]
一切只需一个循环!
而且只有一个直接赋值 ;)
它也是一次构建键数组,而不是先计算最小值,然后用这个键过滤所有元素。
const obj = { thingA: 1, thingB: 2, thingC: 1, thingD: 1, thingE: 4 }
let rand = Object.entries(obj).reduceRight((a,[k,v],idx)=>
{
if (v < a.min) { a.min = v; a.res.length = 0 }
if (v === a.min) { a.res.push(k) }
if (idx) return a
else return a.res[Math.floor(Math.random()*a.res.length)]
},{ min:Infinity, res:[] } )
console.log( rand )
一个循环,与通过 reduce 循环然后遍历对象键相比。
const obj = {
thingA: 1,
thingB: 2,
thingC: 1,
thingD: 1,
thingE: 4
}
function randomizeKey(_obj) {
let objKeysArr = [];
let minValue;
for (const property in _obj) {
if (minValue == undefined || _obj[property] < minValue) {
objKeysArr = [];
minValue = _obj[property];
}
if (_obj[property] == minValue) {
objKeysArr.push(property);
}
}
console.log(`all keys: ${objKeysArr}`);
return objKeysArr[randomize(objKeysArr.length)];
}
function randomize(max, min) {
return Math.floor(Math.random() * max) + (min || 0);
}
console.log( randomizeKey(obj) );
我有一个解决方案,但我不确定这是否是最好的方法..我有一个看起来像这样的对象:
const obj = {
thingA: 1,
thingB: 2,
thingC: 1,
thingD: 1,
thingE: 4
}
我想获取所有具有最低值的键值对,然后能够随机 select 具有最低值的键值对之一(在本例中随机 select 三个键中的一个值为 1)。
这就是我所拥有的并且有效,但我想知道是否有一种 better/more 有效的方法:
const keyArray = Object.keys(obj)
const minArray = (keyArray) => {
const min = keyArray.reduce((prev, curr) => obj[curr] ? prev : curr);
const res = [];
for (const key of keyArray) {
if (obj[key] !== obj[min]) {
continue
}
res.push(key)
}
return res
}
const randomMinKey = minArray(keyArray)[Math.floor(Math.random()*minArray.length)]
一切只需一个循环! 而且只有一个直接赋值 ;)
它也是一次构建键数组,而不是先计算最小值,然后用这个键过滤所有元素。
const obj = { thingA: 1, thingB: 2, thingC: 1, thingD: 1, thingE: 4 }
let rand = Object.entries(obj).reduceRight((a,[k,v],idx)=>
{
if (v < a.min) { a.min = v; a.res.length = 0 }
if (v === a.min) { a.res.push(k) }
if (idx) return a
else return a.res[Math.floor(Math.random()*a.res.length)]
},{ min:Infinity, res:[] } )
console.log( rand )
一个循环,与通过 reduce 循环然后遍历对象键相比。
const obj = {
thingA: 1,
thingB: 2,
thingC: 1,
thingD: 1,
thingE: 4
}
function randomizeKey(_obj) {
let objKeysArr = [];
let minValue;
for (const property in _obj) {
if (minValue == undefined || _obj[property] < minValue) {
objKeysArr = [];
minValue = _obj[property];
}
if (_obj[property] == minValue) {
objKeysArr.push(property);
}
}
console.log(`all keys: ${objKeysArr}`);
return objKeysArr[randomize(objKeysArr.length)];
}
function randomize(max, min) {
return Math.floor(Math.random() * max) + (min || 0);
}
console.log( randomizeKey(obj) );