如何在Swift中找到2d数组中所有元素的总和?(How to find the summation of all elements in 2d array in Swift?)
假设我有以下2d数组:
let my2dArray = [[01, 02, 03, 04], [05, 06, 07, 08], [09, 10, 11, 12], [13, 14, 15, 16]]
获取
my2dArray
中所有元素总和的最简单方法是什么?假设输出应该是:
01 + 02 + 03 + 04 + 05 + 06 + 07 + 08 + 09 + 10 + 11 + 12 + 13 + 14 + 15 + 16 = 136
Let's say that I have the following 2d array:
let my2dArray = [[01, 02, 03, 04], [05, 06, 07, 08], [09, 10, 11, 12], [13, 14, 15, 16]]
What is the easiest way to get the summation of all elements in
my2dArray
?Assuming that the output should be:
01 + 02 + 03 + 04 + 05 + 06 + 07 + 08 + 09 + 10 + 11 + 12 + 13 + 14 + 15 + 16 = 136
原文:https://stackoverflow.com/questions/41292792
更新时间:2024-03-11 22:03
最满意答案
如果它是第一个元素,您可以尝试使用
each
索引并仅调用回调就像是:
return this.each(function(i) { .... .... complete: function() { i==0 && a.callback && a.callback() }
You could try using the index of
each
and only call the callback if it is the first elementSomething like:
return this.each(function(i) { .... .... complete: function() { i==0 && a.callback && a.callback() }
相关问答
更多-
允许多个同时动画的jQuery方法都非常好,但是当你的动画都是连续的时候它就太过分了。 我建议创建一个动画描述数组,然后在一个简单的循环中一次调用一个。 每个描述都需要知道要设置动画的元素,调用哪个函数以及函数参数是什么。 暂停动画只是一个不循环的问题: function next() { if (!animating) return; var a = list.shift(); if (a) { var el = a[0]; var fn = a[ ...
-
animate将呼叫一次为您呼叫animate的集合中的每个元素 : 如果提供,则在每个元素的基础上调用start , step , progress , complete , done , fail和always callback。 由于您正在动画化两个元素( html元素和body元素),所以您将获得两个回调。 (对于任何人想知道为什么OP是动画两个元素,这是因为动画在一些浏览器的body上工作,而在其他浏览器上的html上)。 要在动画完成时获得单个回调, animate文档将animate您使用pr ...
-
为什么在动画完成之前调用我的jQuery完整函数?(Why is my jQuery complete function called before animate is finish?)[2023-11-15]
var callback = function(n,i) { $.MyObject.addDiv(n, i); alert("wtf"); }(name, index); 你在这里调用回调,这就是为什么它被调用。 尝试这样做,我认为名称和索引应该由于关闭而存在。 function MyUberObject(data) { ... this.add = function(name, index) { var callback = ... -
使用jQuery的.delay()方法在所需的延迟后启动.delay()的动画。 $('#a').on('click', function() { $('#b').animate({opacity: 0}, 1000); $('#c').delay(500).animate({opacity: 0}, 1000); }); 编辑 这两种动画都是完整的,有两种方法可以做。 原油: $('#a').on('click', function() { $('#b').animate({op ...
-
问题是我的CSS。 我在我的过滤器面板上使用了此规则: transition-duration: .2s; -moz-transition-duration: .2s; -webkit-transition-duration: .2s; 所以我很确定CSS动画是错误的JQuery,在JQuery动画完全完成之前触发“完成”事件。 一旦我删除了我的CSS代码的一部分,一切都按预期工作。 感谢您的帮助,并再次感到遗憾,因为我浪费了这些时间。 Problem was my CSS. I had this rul ...
-
正如Chad所说,$ .when允许你传递一堆回调,一旦所有这些回调完成,完成将会触发。 http://vimeo.com/22687950是关于延迟的视频,大概是15分钟的标记$ .when()被引入。 大约20分钟你会看到这段代码: var $balls = $(".bounceDemo"), a = $balls.eq(0).drop(500), b = $balls.eq(1).drop(2000), c = $balls.eq(2).drop(4000); ...
-
你做了什么FormatAndUpload(authResult,data); 是错的 传递回调的正确方法是 .then(function(authResult){ FormatAndUpload(authResult, data); }, errorCallback); 所以你的saveToSharepoint将是这样的 function saveToSharepoint(data) { var authority = "https://login.microsoftonline.com/ ...
-
延迟对象回调挂钩的JQuery执行顺序(承诺与完成)(JQuery Execution Order of Deferred Object Callback Hooks (Promise vs. Complete))[2022-09-01]
jQuery Deferred在版本3.0( https://blog.jquery.com/2016/06/09/jquery-3-0-final-released/ )中更新,因此请确保您的jQuery版本与小提琴版本相同( 3.2.1)可能有所帮助。 jQuery Deferred was updated in version 3.0 (https://blog.jquery.com/2016/06/09/jquery-3-0-final-released/), so making sure your ... -
回调:animate complete:在.promise()后调用。done()(Callbacks : animate complete: called after .promise().done())[2022-09-20]
如果它是第一个元素,您可以尝试使用each索引并仅调用回调 就像是: return this.each(function(i) { .... .... complete: function() { i==0 && a.callback && a.callback() } You could try using the index of each and only call the callback if it is the first element Somethi ... -
当您希望在与元素集合相关的所有动画完成时收到通知时,您可以使用promise : $(".container, .selFrame").promise().then(function(){ // all finished // we may change a boolean but usually we would call a callback }); 例如,这可以在你的if/else 。 When you want to be notified when all animati ...