如何在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 element

Something like:

return this.each(function(i) {
   ....
   ....
   complete: function() {
        i==0 && a.callback &&  a.callback()
  }

相关问答

更多

jquery stop()所有动画都没有让延迟$ .when,.promise(),. done()返回完成?(jquery stop() all animations without letting deferreds $.when, .promise(), .done() return complete?)

允许多个同时动画的jQuery方法都非常好,但是当你的动画都是连续的时候它就太过分了。 我建议创建一个动画描述数组,然后在一个简单的循环中一次调用一个。 每个描述都需要知道要设置动画的元素,调用哪个函数以及函数参数是什么。 暂停动画只是一个不循环的问题: function next() { if (!animating) return; var a = list.shift(); if (a) { var el = a[0]; var fn...

回调.animate()被调用两次jquery(Callback of .animate() gets called twice jquery)

animate将呼叫一次为您呼叫animate的集合中的每个元素 : 如果提供,则在每个元素的基础上调用start , step , progress , complete , done , fail和always callback。 由于您正在动画化两个元素( html元素和body元素),所以您将获得两个回调。 (对于任何人想知道为什么OP是动画两个元素,这是因为动画在一些浏览器的body上工作,而在其他浏览器上的html上)。 要在动画完成时获得单个回调, animate文档将animate...

为什么在动画完成之前调用我的jQuery完整函数?(Why is my jQuery complete function called before animate is finish?)

var callback = function(n,i) { $.MyObject.addDiv(n, i); alert("wtf"); }(name, index); 你在这里调用回调,这就是为什么它被调用。 尝试这样做,我认为名称和索引应该由于关闭而存在。 function MyUberObject(data) { ... this.add = function(name, index) { var callba...

动画正在进行时的jquery回调(jquery callback when animate in progress)

使用jQuery的.delay()方法在所需的延迟后启动.delay()的动画。 $('#a').on('click', function() { $('#b').animate({opacity: 0}, 1000); $('#c').delay(500).animate({opacity: 0}, 1000); }); 编辑 这两种动画都是完整的,有两种方法可以做。 原油: $('#a').on('click', function() { $('#b').animat...

动画完成之前的JQuery Animate回调触发(JQuery Animate callback firing before animation is complete)

问题是我的CSS。 我在我的过滤器面板上使用了此规则: transition-duration: .2s; -moz-transition-duration: .2s; -webkit-transition-duration: .2s; 所以我很确定CSS动画是错误的JQuery,在JQuery动画完全完成之前触发“完成”事件。 一旦我删除了我的CSS代码的一部分,一切都按预期工作。 感谢您的帮助,并再次感到遗憾,因为我浪费了这些时间。 Problem was my CSS. I had thi...

使用新的jQuery 1.6 $ .animate“promise”和回调之间的区别?(Difference between using the new jQuery 1.6 $.animate “promise” and a callback?)

正如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(4...

在Promise完成之前调用的方法(Method being called before Promise is complete)

你做了什么FormatAndUpload(authResult,data); 是错的 传递回调的正确方法是 .then(function(authResult){ FormatAndUpload(authResult, data); }, errorCallback); 所以你的saveToSharepoint将是这样的 function saveToSharepoint(data) { var authority = "https://login.microsoftonline...

延迟对象回调挂钩的JQuery执行顺序(承诺与完成)(JQuery Execution Order of Deferred Object Callback Hooks (Promise vs. Complete))

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...

回调:animate complete:在.promise()后调用。done()(Callbacks : animate complete: called after .promise().done())

如果它是第一个元素,您可以尝试使用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 So...

如何分组2个jQuery动画的完整回调?(How to group 2 jQuery animation's complete callbacks?)

当您希望在与元素集合相关的所有动画完成时收到通知时,您可以使用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 an...

相关文章

更多

could not find system property or JNDI

Thanks everyone!! Finally got a solution for this p ...

Swift入门视频教程-尚学堂视频教程

最新Swift语言语法介绍,包括Swift流程控制语句、Swift各种构造函数、closure、泛型、 ...

Phaser开源2d引擎 javascript/html5游戏框架

原文来自:http://html6game.com/thread-1023-1-1.shtml 转自: ...

移动MM failed to find resource file{mmiap.xml}

原地址:http://blog.csdn.net/alking_sun/article/details ...

幻世(OurDream)2D图形引擎易语言汉化版更新提示

幻世引擎的易语言汉化专版到目前为止已经累积了多个BUG,其中多个BUG是影响引擎功能使用的问题,我将会 ...

幻世(OurDream)2D图形引擎大更新——炫丽粒子特效强势回归!

本次更新终于让各位期待已久的绚丽粒子系统特效强势回归到幻世当中了。凭借新引擎强大而又高效的绘图,新的粒 ...

千锋首发Swift视频教程

千锋Swift视频教程-7.Swift结构体.mp4 千锋Swift视频教程-13.代理反向传值.mp ...

3d引擎列表

免费引擎 Agar - 一个高级图形应用程序框架,用于2D和3D游戏。 Allegro lib ...

java3d的前景

奥运后一直在找工作,本来是做WEB方面的工作。 就像北京今天的天气一样,最近IT也是冷的要命。 工 ...

JOGL 处理3D图形

GLAutoDrawable&nbsp

最新问答

更多

获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)

我用Google搜索了一个解决方案。 “EnumDisplayModeProvider”是我自己设置网站的各种模式的枚举。 public EnumDisplayModeProvider GetDisplayModeId() { foreach (var mode in DisplayModeProvider.Instance.Modes) if (mode.CanHandleContext(HttpContext)) {

如何通过引用返回对象?(How is returning an object by reference possible?)

这相对简单:在类的构造函数中,您可以分配内存,例如使用new 。 如果你制作一个对象的副本,你不是每次都分配新的内存,而是只复制指向原始内存块的指针,同时递增一个也存储在内存中的引用计数器,使得每个副本都是对象可以访问它。 如果引用计数降至零,则销毁对象将减少引用计数并仅释放分配的内存。 您只需要一个自定义复制构造函数和赋值运算符。 这基本上是共享指针的工作方式。 This is relatively easy: In the class' constructor, you allocate m

矩阵如何存储在内存中?(How are matrices stored in memory?)

正如它在“熵编码”中所说的那样,使用Z字形图案,与RLE一起使用,在许多情况下,RLE已经减小了尺寸。 但是,据我所知,DCT本身并没有给出稀疏矩阵。 但它通常会增强矩阵的熵。 这是compressen变得有损的点:输入矩阵用DCT传输,然后量化量化然后使用霍夫曼编码。 As it says in "Entropy coding" a zig-zag pattern is used, together with RLE which will already reduce size for man

每个请求的Java新会话?(Java New Session For Each Request?)

你是如何进行重定向的? 您是否事先调用了HttpServletResponse.encodeRedirectURL()? 在这里阅读javadoc 您可以使用它像response.sendRedirect(response.encodeRedirectURL(path)); The issue was with the path in the JSESSIONID cookie. I still can't figure out why it was being set to the tomca

css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)

我认为word-break ,如果你想在一个单词中打破行,你可以指定它,这样做可以解决问题: .column { word-break:break-all; } jsFiddle演示。 您可以在此处阅读有关word-break属性的更多信息。 I think word-break, with which you can specify if you want to break line within a word, will do the trick: .column { word-break

无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)

我认为您忘记在分类时间内缩放输入图像,如train_test.prototxt文件的第11行所示。 您可能应该在C ++代码中的某个位置乘以该因子,或者使用Caffe图层来缩放输入(请查看ELTWISE或POWER图层)。 编辑: 在评论中进行了一次对话之后,结果发现在classification.cpp文件中错误地删除了图像均值,而在原始训练/测试管道中没有减去图像均值。 I think you have forgotten to scale the input image during cl

xcode语法颜色编码解释?(xcode syntax color coding explained?)

转到: Xcode => Preferences => Fonts & Colors 您将看到每个语法高亮颜色旁边都有一个简短的解释。 Go to: Xcode => Preferences => Fonts & Colors You'll see that each syntax highlighting colour has a brief explanation next to it.

在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)

你考虑过第三方拼写检查吗? 您可以将在C#中开发的自定义WinForms控件插入访问数据库吗? VB6控件怎么样? 如果你能找到一个使用第三方库进行拼写检查的控件,那可能会有效。 Have you considered a third party spell checker? Can you insert a custom WinForms controls developed in C# into an access database? What about a VB6 control? If

从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)

我有同样的问题,因为我在远程服务器上有两个图像,我需要在每天的预定义时间复制到我的本地服务器,这是我能够提出的代码... try { if(@copy('url/to/source/image.ext', 'local/absolute/path/on/server/' . date("d-m-Y") . ".gif")) { } else { $errors = error_get_last(); throw new Exception($err

从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))

我不确定我完全明白你在说什么。 你能编辑你的帖子并包含你正在做的Subversion命令/操作的特定顺序吗? 最好使用命令行svn客户端,以便容易为其他人重现问题。 如果您只是想获取文件的旧副本(即使该文件不再存在),您可以使用如下命令: svn copy ${repo}/trunk/moduleA/file1@${rev} ${repo}/trunk/moduleB/file1 其中${repo}是您的存储库的URL, ${rev}是您想要的文件的版本。 这将恢复该文件的旧版本,包括最高版本