【问题标题】:How to add the first element of this nested array如何添加这个嵌套数组的第一个元素
【发布时间】:2020-07-24 02:09:29
【问题描述】:

我有这个嵌套数组

let thirarray = [
    [2, 3, 4],
    [5, 6, 7, 8],
    [9, 10],
];

我想要做的是添加每个数组的第一个元素,所以基本上添加 2+5+9= 16 我知道我可以像这样访问每个元素

//this is how i can access the first element of each array
console.log(thirarray[0][0]);
console.log(thirarray[1][0]);
console.log(thirarray[2][0]);

我知道我可以使用嵌套循环访问所有这样的元素

let suminsidearrays = 0;

for (i = 0; i < thirarray.length; i++) {
    for (let j = 0; j < thirarray[i].length; j++) {
        console.log(thirarray[i][j]);
        suminsidearrays += thirarray[i][j];
        console.log(suminsidearrays);

    }
}

所以我的问题是如何添加每个数组的第一个元素?

【问题讨论】:

    标签: javascript multidimensional-array


    【解决方案1】:

    最简单的方法

    let thirarray = [[2, 3, 4],[5, 6, 7, 8],[9, 10]];
    var res=0
    thirarray.forEach(a=>res+=a[0])
    console.log(res)

    【讨论】:

      【解决方案2】:

      这将对主数组中每个数组的第一个元素求和。

      const array = [[2, 3, 4],[5, 6, 7, 8],[9, 10]]
      let sum = array.reduce((a,c)=>a+c[0],0)
      console.log(sum)

      更新评论中的新模式要求。

      const array = [[2, 3, 4],[5, 6, 7, 8],[9, 10, 11]]
      let i = 0, sum = array.reduce((a,c)=>a+c[i++],0)
      console.log(sum)

      【讨论】:

      • 非常感谢您帮助我。效果很好,我可以再问你一件事。假设我将数字 11 添加到最后一个数组,并尝试像这样 2+6+11 = 19 逐步添加数字。我该如何实现呢?我正在尝试了解如何以静态方式和渐进方式添加元素。
      • @Cosmel 想要的输出是什么?
      • 我现在正在寻找的输出是 19 ,它加上 2 + 6 + 11 = 19 。第一个数组中的第一个元素然后编号为 6,这将是第二个数组中的索引位置 1,然后是最后一个数组中的 11,它应该是索引位置 2。
      • @Cosmel 这里的模式到底是什么?
      • 我看到模式在每个数组上增加 1。您的解决方案不仅帮助我在每个数组中添加第一个元素,而且我能够将它用于下一个索引位置以及添加 3+6+10=19。我想知道的是如何现在添加第一个元素,然后将 1 添加到下一个索引位置。所以我应该有 2+6+11。再次将数字 11 添加到最后一个数组
      【解决方案3】:

      我只想补充一点,我能够使用从 @holydragon 那里学到的东西来解决它。 所以这就是我所做的

      let fourthrarray = [
          [20, 30, 40],
          [50, 60, 70, 80],
          [90, 100, 110, 120, 130],
      ];
      
      let nextindexposition = 0;
      let nextto = 0;
      let sumeelsegundo = 0;
      
      for (let i = 0; i < fourthrarray.length; i++) {
          //this will allow me to see the 1st index position from each array
          //i should expect to see 20,50,90
          sumeelsegundo = fourthrarray[i][0];
         //this will add the first index position of each array expected output will be 160
          sumeelsegundo += fourthrarray[i][0];
          console.log(sumeelsegundo);
         //in order to add the next index position from each array i used two variable
        //one that will store the result and the other one that will increase the index 
        //position by one on every iteration. this one will allow me to see the elements
       //that will be added. expected elements will be 20,60,110
          nextto = fourthrarray[i][nextindexposition++];
       //this will be adding the elements expected output expected output 190
          nextto += fourthrarray[i][nextindexposition++];
          console.log(nextto);
      
      }
      

      只是为了澄清一下这个解决方案适用于添加数字,如果我想相乘,我只需将变量 nextto 的起始值从 0 更改为 1。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-21
        • 2020-07-14
        • 1970-01-01
        • 1970-01-01
        • 2021-04-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多