[英]Sum all same elements in one array
how can I sum all same elements in one array? 如何将一个数组中的所有相同元素求和? For example I have an array: 例如我有一个数组:
[20,20,20,10,10,5,1]
How can I make it [60,20,5,1]
? 我怎样才能做到[60,20,5,1]
?
Here's what I have tried so far: 到目前为止,这是我尝试过的:
var money = [20, 20, 20, 10, 10, 5, 1];
for (var i = 0; i < money.length; i++) {
if (money[i] == money[i + 1]) {
money[i] += money[i + 1];
money.splice(money.indexOf(money[i + 1]), 1);
}
}
I would do something like this: 我会做这样的事情:
Snippet 片段
// Our original array. var arr = [20, 20, 20, 10, 10, 5, 1]; // Let's have a counts object that stores the counts. var counts = {}; // Loop through the array to get the counts. for (var i = 0; i < arr.length; i++) { var num = arr[i]; counts[num] = counts[num] ? counts[num] + 1 : 1; } // Have a final array. var fin = []; // Multiply the count with the values and push it to the final array. for (var count in counts) { fin.push(counts[count] * count); } console.log(fin);
Use Array#reduce
method with a variable to store previous element. 使用带有变量的Array#reduce
方法存储先前的元素。
var arr = [20, 20, 20, 10, 10, 5, 1]; // variable for storing previous element var prev; var res = arr.reduce(function(arr, v) { // if element is same as previous then add // value with last element if (prev == v) arr[arr.length - 1] += v; // else push and update prev variable else arr.push(prev = v) // return the array refernece return arr; // set initial value as empty array for result }, []) console.log(res);
UPDATE : If same values are not adjacent then use an object to refer the index. 更新:如果不相邻的值相同,则使用一个对象引用索引。
var arr = [20, 20, 20, 10, 10, 5, 1]; // object for refering index var ref = {}; var res = arr.reduce(function(arr, v) { // check property is defined or not if // defined update value at the index if (ref.hasOwnProperty(v)) arr[ref[v]] += v; else { // else add property to object and push element ref[v] = arr.length; arr.push(prev = v) } // return array reference return arr; // set initial value as empty array for result }, []) console.log(res);
var list= [20,20,20,10,10,5,1];
var result=[];
//index of already added values
var listOfIndex=[];
for(var i=0;i<list.length;i++){
if(listOfIndex.indexOf(i)>=0){
continue;
}
var number=list[i];
for(var j=i+1;j<list.length;j++){
if(list[i]==list[j]){
number = number+list[j];
listOfIndex.push(j);//push in this list the index of the value that has been added
}
}
result.push(number);
}
console.log(result);
You could use a hash table and store the index of the result slot. 您可以使用哈希表并存储结果槽的索引。 This works for unsorted values as well. 这也适用于未排序的值。
var data = [20, 20, 20, 10, 10, 5, 1], result = []; data.forEach(function (a) { if (!(a in this)) { this[a] = result.push(0) - 1; } result[this[a]] += a; }, Object.create(null)); console.log(result);
Another single-loop proposal using Array.prototype.reduce
and a hash table
that store the indices the result array being created - will handle input that is not sorted too. 另一个使用Array.prototype.reduce
单循环建议和一个存储创建的结果数组索引的hash table
-将处理也未排序的输入。
See demo below: 请参见下面的演示:
var array = [20, 20, 20, 10, 10, 5, 1]; var result = array.reduce(function(hash){ return function(p,c) { if(c in hash) { p[hash[c]] += c; } else { // store indices in the array hash[c] = p.push(c) - 1; } return p; }; }(Object.create(null)),[]); console.log(result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.