JavaScript: Trouble with reduce
JavaScript: Trouble with reduce
我试图在一个较大的组中获得一个数组的和。代码更新第一个数组中的第一个数组值,但是reduce函数不起作用。下面是代码:
JavaScript (HTML查看结果):
var boughtRaw = [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0]];
var boughtCalc = function (level) {
boughtRaw[0[0]] = 5
var Raw = boughtRaw[level];
window["boughtList" + level] = Raw.reduce(function(a, b) {
return a + b;}, 0);
};
boughtCalc(0);
document.getElementById("debug").innerHTML = boughtList0;
/* Should return 5 */
<span id="debug" style="border: 1px dashed red">Debug</span>
您必须将boughtRaw[0[0]] = 5
更正为这一行boughtRaw[0][0] = 5
。
var boughtRaw = [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0]];
var boughtCalc = function (level) {
boughtRaw[0][0] = 5; // corrected
var Raw = boughtRaw[level];
window["boughtList" + level] = Raw.reduce(function(a, b) {
return a + b;}, 0);
};
boughtCalc(0);
document.getElementById("debug").innerHTML = boughtList0;
/* return 5 */
<span id="debug" style="border: 1px dashed red">Debug</span>
访问数组的元素(of array ..)数组)你必须放置[]...[]
相邻的,不像这个[[...[]]]
。
var element1 = array[0] // get first element of array
var element2 = element1[0] // get first element of element1
.....
var result = elementN[0] // get first element of elementN
可以用
代替var result = array[0][0]...[0].
相关文章:
- Fighting with FRP
- issue with FB.Event.subscribe
- geolocation-marker.js conflict with markerclusterer.js
- Angular 2.0 with JavaScript or TypeScript?
- 为什么不推荐使用“with”?是否有更好或其他方法可以“下降”到对象的命名空间
- WebComponentsJS with IE10
- 对一个对象使用reduce可以返回一个没有't在数组中包含目标字母
- 指令的模板必须只有一个根元素:With restrict E&替换true
- timeago.js with datatable and PHP
- Ajax and Json with Rails
- errors with Javascript try catch
- how to split a string with ','
- Understanding Javascript scope with "var that = this&qu
- Architecture for CPU intensive tasks with NodeJS & Socke
- How to declare a Map containing certain properties with flow
- 设置'这'在React with Inverse Data Flow中
- 节点中的Map Reduce
- MongoDB Reduce Key with single value
- Javascript: Reduce function with ||
- JavaScript: Trouble with reduce