【问题标题】:Get the sum of all specified elements in an array of objects获取对象数组中所有指定元素的总和
【发布时间】:2017-09-23 12:33:28
【问题描述】:

我有一个对象数组如下

[
    {"width":128.90663423245883,"height":160,"X":0,"Y":140},
    {"width":277.0938568683375,"height":263,"X":128.90663423245883,"Y":37},
    {"width":264.8267031014369,"height":261,"X":277.0938568683375,"Y":39},
    {"width":229.14003389179788,"height":60,"X":264.8267031014369,"Y":240},
    {"width":10.032771905968888,"height":177,"X":229.14003389179788,"Y":123}
]

我正在寻找一个函数来获取当前对象中所有“宽度”元素的总和。

类似:

function getAllBefore(current) {
    // here i want to get the sum of the previous 4 'width' elements in the object
}
getAllBefore(obj[5]);

【问题讨论】:

    标签: javascript arrays object multidimensional-array


    【解决方案1】:
    function count(stack) {
        var totWidth = 0;
        stack.forEach(function(element) {
          totWidth = totWidth+element.width;
        });
      return totWidth;
    }
    

    工作example

    【讨论】:

      【解决方案2】:

      如果您在不知道元素索引但只想发送对象的情况下寻找解决方案,那么您需要检查当前项目的所有属性以及可用项目,您可以这样做:

      var items = [
      {"width":128.90663423245883,"height":160,"X":0,"Y":140},
      {"width":277.0938568683375,"height":263,"X":128.90663423245883,"Y":37},
      {"width":264.8267031014369,"height":261,"X":277.0938568683375,"Y":39},
      {"width":229.14003389179788,"height":60,"X":264.8267031014369,"Y":240},
      {"width":10.032771905968888,"height":177,"X":229.14003389179788,"Y":123}
      ];
      
      function getAllBefore(current) {
          var sum = 0;
      
          for (var i = 0; i < items.length; i++) {
              var item = items[i];
              if (current.width == item.width && current.height == item.height && current.X == item.X && current.Y == item.Y)
              {
                   return sum;
              }
      
              sum += item.width;
          }
      }
      
      getAllBefore(items[2]);
      

      【讨论】:

        【解决方案3】:

        让我们在 JavaScript 中使用reduce

        var arr = [
            {"width":128.90663423245883,"height":160,"X":0,"Y":140},
            {"width":277.0938568683375,"height":263,"X":128.90663423245883,"Y":37},
            {"width":264.8267031014369,"height":261,"X":277.0938568683375,"Y":39},
            {"width":229.14003389179788,"height":60,"X":264.8267031014369,"Y":240},
            {"width":10.032771905968888,"height":177,"X":229.14003389179788,"Y":123}
        ];
        
        console.log(arr.reduce((acc, b) => acc + b.width, 0.0));

        【讨论】:

          【解决方案4】:

          这是一个示例,使用切片首先返回具有正确长度的数组,然后使用 reduce 来返回总和

          const getSum = (objNum, arr) => {
            const newArr =  arr.slice(0, objNum - 1)
            return newArr.reduce((current, next) => {
              return current + next.width;
            }, 0)
          }
          

          在 ES5 中

          var getSum = (objNum, arr) => {
            vat newArr =  arr.slice(0, objNum - 1)
            return newArr.reduce(function(current, next) {
              return current + next.width;
            }, 0)
          }
          

          在 1 行

          const getSum = (objNum, arr) => arr.slice(0, objNum - 1).reduce((current, next) =>  current + next.width, 0)
          

          【讨论】:

            【解决方案5】:

            为了更容易和更可重用的代码传递给方法对象和索引,如下:

            function getAllBefore(obj, index){
              var sum=0;
              for(var i=0; i<index; i++){
                sum+=obj[i].width;
              }
            
              return sum;
            }
            

            然后这样称呼它:

            getAllBefore(obj, 5);
            

            【讨论】:

              猜你喜欢
              • 2021-06-01
              • 2019-09-20
              • 1970-01-01
              • 1970-01-01
              • 2020-02-22
              • 1970-01-01
              • 2023-02-07
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多