【JS】对象数组根据某个属性求和

arrObj = [{
              title: '序号',
              width:60,
          },
          {
              title: '商品编码',
              width:80,

          }]


    //计算对象数组中某个属性合计
    countTotal = (arr, keyName) => {
        let $total = 0;
        $total = arr.reduce(function (total, currentValue, currentIndex, arr){
            return currentValue[keyName] ? (total + currentValue[keyName]) : total;
        }, 0);
        return $total;
    }



console.log(countTotal(arrobj, 'width'))

你可能感兴趣的:(JS实例,javascript,vue.js,elementui)