我有一个对象数组,如下所示
[
{"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]);