gpt4 book ai didi

javascript - 使用 Javascript 计算 Json 中特定属性的总和

转载 作者:行者123 更新时间:2023-12-01 01:24:30 24 4
gpt4 key购买 nike

我正在使用 Asp.net MVC ,并且从我的 ActionResult 中我使用 TempData 返回数据。像这样

 TempData["chartmodel"] = new
{
Labels = res.Select(x => x.ServiceName).ToList(), //changes hotel name to service
BadData = res.Select(x => x.Development).ToList(),
ExcellentData = res.Select(x => x.Excellent).ToList(),
GoodData = res.Select(x => x.Average).ToList(),
VeryGoodData = res.Select(x => x.Good).ToList(),
PoorData = res.Select(x => x.unsatisfactory).ToList(),
};
return View(res);

在我看来,我将其编码为 json

 var chartModel = @Html.Raw(Json.Encode(TempData["chartmodel"]));

现在我只想对优秀的属性(property)进行求和。假设我有数据

chartModel.ExcellentData = 2,3,5

我想将其在变量中求和为10

最佳答案

使用减少:

var sumVariable = chartModel.ExcellentData.reduce(function(e, a) { return e + a; }, 0)

关于javascript - 使用 Javascript 计算 Json 中特定属性的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53929064/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com