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]
?
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.
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.
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.