javascript - 使用Javascript的Json中特定属性的总和
问题描述
我正在使用 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"]));
现在我只想总结优秀的财产。假设我有数据
chartModel.ExcellentData =2,3,5
我想将它总结为一个变量10
解决方案
使用reduce
:
var sumVariable = chartModel.ExcellentData.reduce(function(e, a) { return e + a; }, 0)
推荐阅读
- css - 如何将提交按钮移动到表单下方
- ios - iOS TTS Alex 语音在实际不可用时被视为可用
- python - 尝试使用 pandas 读取 csv 文件时出错
- python - Pandas 中一列中从一个值到另一个值的转换计数
- python - f-string将舍入的浮点数恢复为未舍入的
- flutter - 我如何将 file.path 用于列表
? - sql - 如何内连接两个匿名表 [SQL]
- c# - .NET 核心中 MySQL.Data 的“无法加载文件或程序集”问题
- python - 如何在 Python 中使用命令行读取腌制文件?
- android - Kotlinx Serialization @Body converter error while using HashMap with Any type