简体   繁体   English

Javascript-从数组中删除元素/对某个值以上的数组中的元素求和

[英]Javascript - Remove elements from an array / sum elements in an array above a certain value

(1) (1)

I have here an array that is a mix of strings and ints: 我这里有一个包含字符串和整数的数组:

var newArr = [ 22, 7, "2761", 16, "91981", "37728", "13909", "247214", "8804", 6, 2 ]

My ultimate goal is to remove the values that are ints, and add the other values. 我的最终目标是删除整数值,然后添加其他值。

One way I tried to achieve this was to first convert all to ints: 我尝试实现此目的的一种方法是先将所有内容都转换为int:

  for (element in newArr) {
    newArr[element] = parseInt(newArr[element], 10);
  }

Is this the best way to do that? 这是最好的方法吗? It seems to output an array of ints like this: 似乎输出这样的整数数组:

var newArr = [ 22, 7, 2761, 16, 91981, 37728, 13909, 247214, 8804, 6, 2 ];

(2) (2)

Then I would like to only sum elements in newArr that are above the value of 30. This is my code: 然后,我只想求和newArr中大于30的元素。这是我的代码:

var newArr = [ 22, 7, 2761, 16, 91981, 37728, 13909, 247214, 8804, 6, 2 ];

  for (element in newArr) {
    if (newArr[element] > 30) {
      sum += newArr[element];
    }
  }

It doesn't seem to be working. 它似乎不起作用。 Please help. 请帮忙。

(3) A final question is: (3)最后一个问题是:

How could I just eliminate the ints from newArr in step (1), as this would negate the need for the other code, I guess. 我想我该如何在步骤(1)中从newArr中消除整数,因为这将不需要其他代码。

A simple solution using only javascript syntax (no jquery) would be appreciated. 仅使用javascript语法(不使用jquery)的简单解决方案将不胜感激。 (unless the overwhelming consensus is that jquery would be a better option) Thanks Javascript Ninjas! (除非压倒性的共识是jquery将是一个更好的选择)谢谢Javascript Ninjas!

First, you might want to look into Array.map . 首先,您可能需要研究Array.map This will allow you to convert them all to ints. 这将允许您将它们全部转换为int。

var result = newArr.map(function(x) {
  return parseInt(x, 10);
});

Then you can use Array.filter to remove any elements less than or equal to 30. 然后,您可以使用Array.filter删除任何小于或等于30的元素。

result = result.filter(function(x) {
  return x > 30;
});

Finally, you can use Array.reduce to sum them all together. 最后,您可以使用Array.reduce将它们加在一起。

sum = result.reduce(function(sum, x) {
  return sum + x;
}, 0);

There are numerous ways of accomplishing this but I like this approach because you can chain it together. 有很多方法可以实现此目的,但是我喜欢这种方法,因为您可以将其链接在一起。

var sum = newArray.map(function(x) {
      return parseInt(x, 10);
    })
    .filter(function(x) {
      return x > 30;
    })
    .reduce(function(sum, x) {
      return sum + x;
    }, 0);

Detect the type then remove them: Here I go through the array, then remove the numbers then go through it again: 检测类型然后删除它们:在这里,我遍历数组,然后删除数字,然后再次遍历它:

var newArr = [22, 7, "2761", 16, "91981", "37728", "13909", "247214", "8804", 6, 2];

for (element in newArr) {
    alert(typeof newArr[element] + ":" + newArr[element]);
}
for (var i = newArr.length; i--;) {
    if (typeof newArr[i] === "number") {
        newArr.splice(i, 1);
    }
}
alert(newArr.length);
for (element in newArr) {
    alert(typeof newArr[element] + ":" + newArr[element]);
}

I think the best approach to do all this in single iteration would be . 我认为在一次迭代中完成所有这些操作的最佳方法是。

  1. Checking each element with typeof item!="number" 使用typeof item检查每个元素!=“ number”
  2. Using Array's Reduce Right : Sample Use: 使用数组的归约权:示例用法:

    [0, 1, 2, 3, 4].reduceRight(function(previousValue, currentValue, index, array) { return previousValue + currentValue; }); [0,1,2,3,4] .reduceRight(function(previousValue,currentValue,index,array){return previousValue + currentValue;});

You can check the previousValue and currentValue with typeof and return the addition if parseInt() return's 30 or more. 您可以使用typeof检查previousValue和currentValue,如果parseInt()返回30或更大,则返回加法。

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

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