简体   繁体   English

如何计算给定数字以下/上方的数组中的元素数(javascript)

[英]How to count the number of elements in an array below/above a given number (javascript)

It's example of array which contains only numbers: 这是仅包含数字的数组示例:

var array = [3.1, 1, 2.2, 5.1, 6, 7.3, 2.1, 9]

EDITED 已编辑

How to count how many(edited from word 'sum') of elements are in array below / above given number eg. 如何计算在给定数字以下/之上的数组中有多少个元素(从单词“ sum”中编辑) '5.25'? '5.25'?

So answer should be 5 elements are below (3.1, 1, 2.2, 5.1, 2.1) and 3 element are above (6, 7.3, 9) 所以答案应该是5元素在(3.1,1,2.2,5.1,2.1)以下, 3元素在(6,7.3,9)以上

Use Array#reduce with an object { above: 0, below: 0 } as initial value. Array#reduce与对象{ above: 0, below: 0 }用作初始值。 On each iteration check each number against the media, and accordingly add 1 to above/below. 在每次迭代中,对照媒体检查每个数字,并在其上/下加1。

Note: This is an array of strings. 注意:这是一个字符串数组。 I've converted them manually to numbers. 我已经将它们手动转换为数字。 I also assume that a number that equals the median should be added to below. 我还假定应该将等于中位数的数字添加到下面。 If you want to skip this numbers, change the comparison to < . 如果要跳过此数字,请将比较更改为<

 var array = [3.1, 1, 2.2, 5.1, 6, 7.3, 2.1, 9]; var median = 5.25; var counts = array.reduce(function(s, n) { s[n <= median ? 'below' : 'above'] += 1; return s; }, { above: 0, below: 0 }); console.log(counts); 

If you must have an array of strings, you can convert to a number in run time using String#parseFloat or the + operator : 如果必须具有字符串数组,则可以在运行时使用String#parseFloat+运算符将其转换为数字:

 var array = ['3.1', '1', '2.2', '5.1', '6', '7.3', '2.1', '9']; var median = 5.25; var counts = array.reduce(function(s, str) { s[+str <= median ? 'below' : 'above'] += 1; return s; }, { above: 0, below: 0 }); console.log(counts); 

No need for reduce, because the result does not change by using reduce, it is still an object. 不需要reduce,因为使用reduce不会改变结果,所以它仍然是一个对象。

 var array = [3.1, 1, 2.2, 5.1, 6, 7.3, 2.1, 9], value = 5.25, counts = { below: 0, equal: 0, above: 0 }; array.forEach(function(v) { counts[{ '-1': 'below', 0: 'equal', 1: 'above' }[Math.sign(v - value)]]++; }); console.log(counts); 

Assuming you are using an array of numbers.. 假设您使用的是数字数组。

I use lodash ...it helps shorten the code & 我使用lodash ...有助于缩短代码并

 var array = [3.1, 1, 2.2, 5.1, 6, 7.3, 2.1, 9]; console.log(_.sum(array)); // _.sum does the addition 
 <script src="https://cdn.jsdelivr.net/lodash/4.14.1/lodash.min.js"></script> 

There are couple of strategies but I will use the one thats easy to read and understand: 有几种策略,但我将使用易于理解的策略:

(1) Create a new array with all elements that fit the criteria aka <=5.25 (1)创建一个新数组,其中所有元素都符合标准<= 5.25

let sampleArr = ['3.1', '1', '2.2', '5.1', '6', '7.3', '2.1', '9'];

let sortedArr = sampleArr.filter(function(e){
     return parseInt(e) <= 5.25
});

Notice I wrote parseInt(e), that converts the string into a number. 注意,我编写了parseInt(e),它将字符串转换为数字。 The values you gave in your array were all strings. 您在数组中输入的值都是字符串。

(2) Now simply get the length of the sortedArr like so: (2)现在简单地获取sortedArr的长度,如下所示:

let finalCount = sortedArr.length;

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM