简体   繁体   English

获取对象数组中所有指定元素的总和

[英]Get the sum of all specified elements in an array of objects

I have an array of objects as folllows 我有以下对象的数组

[
    {"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}
]

I am looking to write a function that gets the sum of all 'width' elements in the object before the current. 我希望编写一个函数,该函数在当前对象之前获取对象中所有“宽度”元素的总和。

Something like: 就像是:

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

For an easier and more reusable code pass to the method the object and the index, as follows: 为了使代码更容易和更可重用,将对象和索引传递给方法,如下所示:

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

  return sum;
}

And call it like this: 并这样称呼它:

getAllBefore(obj, 5);

Here is an example using a slice to first return the array with the correct length and the a reduce to return the sum 这是一个使用切片首先返回正确长度的数组并使用a减少返回总和的示例

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

and in ES5 在ES5中

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

and in 1 line 并在1行中

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

Let's use reduce in JavaScript 让我们在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)); 

If you are looking for solution without knowing index of element but you want only to send object, then you will need to check all properties of current item with available items and you can do it like this: 如果您在不知道元素索引的情况下寻找解决方案,而只想发送对象,那么您将需要检查当前项目的所有属性以及可用的项目,您可以这样进行:

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]);
function count(stack) {
    var totWidth = 0;
    stack.forEach(function(element) {
      totWidth = totWidth+element.width;
    });
  return totWidth;
}

working example 工作实例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM