admin管理员组文章数量:1331640
I want to take the elements of an array, multiply them by their index position, and add the results together.
So if I had:
array = [1, 2, 3]
I would get 8.
// (1 * 0)+(2 * 1)+(3 * 2) = 8
What is the best way to do this in vanilla JS, simply for learning purposes?
I want to take the elements of an array, multiply them by their index position, and add the results together.
So if I had:
array = [1, 2, 3]
I would get 8.
// (1 * 0)+(2 * 1)+(3 * 2) = 8
What is the best way to do this in vanilla JS, simply for learning purposes?
Share Improve this question edited Nov 16, 2016 at 20:13 Blorgbeard 104k50 gold badges235 silver badges276 bronze badges asked Nov 16, 2016 at 20:12 cj_bcj_b 131 silver badge3 bronze badges 4- 5 For learning purposes, you should probably try doing it yourself. Start with a for-loop. – Blorgbeard Commented Nov 16, 2016 at 20:14
-
1
var t = 0, i = array.length; while(i--) t+= i * array[i];
– I wrestled a bear once. Commented Nov 16, 2016 at 20:17 -
1
[1, 2, 3].reduce((s, n, i) => s + (n * i), 0);
– Yoshi Commented Nov 16, 2016 at 20:18 - Thanks you so much guys! My post for someone didn't include the part where I was frustrated over this for hours... but this morning I figured it out, using two for-loops, in the most convoluted was possible. But it works. So thank you for showing me the best way to do it! – cj_b Commented Nov 17, 2016 at 15:47
6 Answers
Reset to default 4A very simple way would be:
console.log(
// ┌ the running total
// │ ┌ the current array value
// │ │ ┌ the current array index
// │ │ │
[1, 2, 3].reduce((s, n, i) => s + (n * i), 0)
);
Manual: Array.prototype.reduce()
A functional, semantic approach would be:
const add = (a, b) => a + b;
const multiply = (a, b) => a * b;
To sum an array:
const sum = a => a.reduce(add, 0);
To add up the products of value and index as you want:
const weightedSum = a => sum(a.map(multiply));
We're able to say a.map(multiply)
because map
happens to pass the value and the index to the callback multiply
.
Of course, this might be more than you want or need to learn at this point. There's nothing wrong with an old-fashioned for loop, as others have mentioned:
function weightedSum(a) {
sum = 0;
for (let i = 0; i < a.length; i++) sum += a[i] * i;
return sum;
}
You can use a map to multiply by index and then use a reduce to obtain the sum
var array = [1, 2, 3]
var result = array.map((x,i) => x*i).reduce((a,b) => a+b,0);
console.log(result);
several approaches, but simplest for you to understand would be:
var numbers = [1,2,3];
var sum = 0;
for(var i = 0;i < numbers.length; i++){
sum+= (i*numbers[i]);
}
console.log(sum);
var array = [1, 2, 3];
var items = array.map(function(x,i){
return x * i;
});
var sum = items.reduce(function(init, x) {
return x + init,0
});// 8
var result = 0;
for(i=0;i<array.length;i++)
{
var val = array[i];
result += val*i;
}
alert(result);
本文标签: javascriptMultiply Array Elements by Index PositionStack Overflow
版权声明:本文标题:javascript - Multiply Array Elements by Index Position - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742271008a2444304.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论