JS查找数组中项目的百分比
JS Find percentage of items in an array
所以我有这个数组:
const colors = ['blue', 'blue', 'red', 'red', 'red', 'green', 'green', 'white']
我想使用JavaScript输出相似项目的百分比。
例如数组中总共 8(100%) 个项目中,蓝色和绿色应各占 25%,红色 37.5% 和白色 12.5%。
我该如何实现?
您首先需要找到每种独特的颜色,然后遍历它们以找出有多少种颜色。一旦你有了这个,你就可以计算百分比为(num * 100 / total)。
看看这个:
const colors = ['blue', 'blue', 'red', 'red', 'red', 'green', 'green', 'white']
const totalItems = colors.length
const uniqueItems = [...new Set(colors)]
uniqueItems.forEach(currColor => {
const numItems = colors.filter(color => color === currColor)
console.log(`color ${currColor} represents ${numItems.length * 100 / totalItems}%`)
})
/*
color blue represents 25%
color red represents 37.5%
color green represents 25%
color white represents 12.5%
*/
这可以帮助:
const colors = ['blue', 'blue', 'red', 'red', 'red', 'green', 'green', 'white']
var data ={}
colors.map(el=>{
if(!data[el]){
return data[el]=colors.filter(ob=>ob===el).length*100/colors.length
}
})
console.log(data)
您可以将一个对象作为散列映射并计算出现次数。然后获取哈希映射的条目和 return 一组颜色和百分比值。
const
colors = ['blue', 'blue', 'red', 'red', 'red', 'green', 'green', 'white'],
percents = Object
.entries(colors.reduce((map, color) => (map[color] = (map[color] || 0) + 1, map), {}))
.map(([color, count]) => [color, count * 100 / colors.length]);
console.log(percents);
.as-console-wrapper { max-height: 100% !important; top: 0; }
到目前为止,其他答案分多个步骤进行。但是使用 reduce
对数据进行单次传递是相当简单的,注意每个实例都会添加 1 / array.length
的一小部分,因此会添加 100 / array.length
的百分比。这是一种技巧:
const percentages = (xs) =>
xs .reduce ((pcts, x) => ({...pcts, [x]: (pcts [x] || 0) + 100 / (xs .length)}), {})
const colors = ['blue', 'blue', 'red', 'red', 'red', 'green', 'green', 'white']
console .log (percentages (colors))
所以我有这个数组:
const colors = ['blue', 'blue', 'red', 'red', 'red', 'green', 'green', 'white']
我想使用JavaScript输出相似项目的百分比。
例如数组中总共 8(100%) 个项目中,蓝色和绿色应各占 25%,红色 37.5% 和白色 12.5%。
我该如何实现?
您首先需要找到每种独特的颜色,然后遍历它们以找出有多少种颜色。一旦你有了这个,你就可以计算百分比为(num * 100 / total)。
看看这个:
const colors = ['blue', 'blue', 'red', 'red', 'red', 'green', 'green', 'white']
const totalItems = colors.length
const uniqueItems = [...new Set(colors)]
uniqueItems.forEach(currColor => {
const numItems = colors.filter(color => color === currColor)
console.log(`color ${currColor} represents ${numItems.length * 100 / totalItems}%`)
})
/*
color blue represents 25%
color red represents 37.5%
color green represents 25%
color white represents 12.5%
*/
这可以帮助:
const colors = ['blue', 'blue', 'red', 'red', 'red', 'green', 'green', 'white']
var data ={}
colors.map(el=>{
if(!data[el]){
return data[el]=colors.filter(ob=>ob===el).length*100/colors.length
}
})
console.log(data)
您可以将一个对象作为散列映射并计算出现次数。然后获取哈希映射的条目和 return 一组颜色和百分比值。
const
colors = ['blue', 'blue', 'red', 'red', 'red', 'green', 'green', 'white'],
percents = Object
.entries(colors.reduce((map, color) => (map[color] = (map[color] || 0) + 1, map), {}))
.map(([color, count]) => [color, count * 100 / colors.length]);
console.log(percents);
.as-console-wrapper { max-height: 100% !important; top: 0; }
到目前为止,其他答案分多个步骤进行。但是使用 reduce
对数据进行单次传递是相当简单的,注意每个实例都会添加 1 / array.length
的一小部分,因此会添加 100 / array.length
的百分比。这是一种技巧:
const percentages = (xs) =>
xs .reduce ((pcts, x) => ({...pcts, [x]: (pcts [x] || 0) + 100 / (xs .length)}), {})
const colors = ['blue', 'blue', 'red', 'red', 'red', 'green', 'green', 'white']
console .log (percentages (colors))