javascript - Json数据中某个字段的最大数字

标签 javascript ecmascript-6 lodash

我的Json数据如下

[{
  "Name": "PieChart",
  "Id": "1",
  "ColumnLocation": "0",
  "RowLocation": "0"
}, {
  "Name": "Calendar",
  "Id": "2",
  "ColumnLocation": "1",
  "RowLocation": "0"
}, {
  "Name": "FavouriteFilter",
  "Id": "3",
  "ColumnLocation": "2",
  "RowLocation": "0"
}, {
  "Name": "FilterResults",
  "Id": "4",
  "ColumnLocation": "0",
  "RowLocation": "1"
}, {
  "Name": "Watched",
  "Id": "5",
  "ColumnLocation": "1",
  "RowLocation": "1"
}]

我需要找到上述数据的ColumnLocation的最大编号。我需要这些信息,因为我可以根据此列数绘制仪表板。

最佳答案

减少:

const a = [{
  "Name": "PieChart",
  "Id": "1",
  "ColumnLocation": "0",
  "RowLocation": "0"
}, {
  "Name": "Calendar",
  "Id": "2",
  "ColumnLocation": "1",
  "RowLocation": "0"
}, {
  "Name": "FavouriteFilter",
  "Id": "3",
  "ColumnLocation": "2",
  "RowLocation": "0"
}, {
  "Name": "FilterResults",
  "Id": "4",
  "ColumnLocation": "0",
  "RowLocation": "1"
}, {
  "Name": "Watched",
  "Id": "5",
  "ColumnLocation": "1",
  "RowLocation": "1"
}];

const r = a.reduce((a, c) => (c.ColumnLocation > a) ? c.ColumnLocation : a, 0);

console.log(r)

关于javascript - Json数据中某个字段的最大数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43767940/

相关文章:

javascript - 检查深层属性(property)值(value)的更简洁方法

javascript - 无法使用 JS 在 Canvas 中移动 Arc

javascript - 带有react-form的嵌套组件react.cloneElement

javascript - 使用lodash在执行setTimeout时触发两个函数

javascript - DefaultRoute 抛出 "Cannot call a class as a function"

javascript - 消息在控制台中安慰两次 - ReactJS

javascript - 如何使用 Lodash/JavaScript 重新格式化此 JSON?

javascript - 如何在响应式(Reactive)表单中获取表单控件的名称?

javascript - 将表单提交推送到数组

javascript - 错误 : write EPIPE