有一个时间数组对象,时次不连续,怎样补全比较好?

发布于 2022-09-07 16:36:41 字数 3133 浏览 34 评论 0

需要展示出一天中所有小时的数据,后端返回的数据是这样
beforData: [

  {
    "time_hours": "09", // 时间
    "approved_amount": 28068.0,
    "pass_rate": 1.0
  },
  {
    "time_hours":"10",
    "approved_amount":0.0,
    "pass_rate":0.0
  },
  {
    "time_hours":"11",
    "approved_amount":42706.0,
    "pass_rate":0.5
  },
  {
    "time_hours":"13",
    "approved_amount":0.0,
    "pass_rate":0.0
  },
  {
    "time_hours":"15",
    "approved_amount":37700.0,
    "pass_rate":0.5714285714
  },
  {
    "time_hours":"16",
    "approved_amount":0.0,
    "pass_rate":0.0
  },
  {
    "time_hours":"17",
    "approved_amount":29520.0,
    "pass_rate":0.6
  },
  {
    "time_hours":"18",
    "approved_amount":0.0,
    "pass_rate":0.0
  },{
    "time_hours":"19",
    "approved_amount":56216.0,
    "pass_rate":1.0
  },
  {
    "time_hours":"22",
    "approved_amount":5800.0,
    "pass_rate":1.0
  },
  {
    "time_hours":"23",
    "approved_amount":0.0,
    "pass_rate":0.0
  }
]

需要将缺少的小时补全,转化成
[

  {
    "time_hours": "01:00",
    "approved_amount": null,
    "pass_rate": null,
  },
  {
    "time_hours": "02:00",
    "approved_amount": null,
    "pass_rate": null,
  },
  {
    "time_hours": "03:00",
    "approved_amount": null,
    "pass_rate": null,
  },
  {
    "time_hours": "04:00",
    "approved_amount": null,
    "pass_rate": null,
  },
  {
    "time_hours": "05:00",
    "approved_amount": null,
    "pass_rate": null,
  },
  {
    "time_hours": "06:00",
    "approved_amount": null,
    "pass_rate": null,
  },
  {
    "time_hours": "07:00",
    "approved_amount": null,
    "pass_rate": null,
  },
  {
    "time_hours": "08:00",
    "approved_amount": null,
    "pass_rate": null,
  },
  {
    "time_hours": "09:00",
    "approved_amount": 28068.0,
    "pass_rate": 1.0
  },
  {
    "time_hours":"10:00",
    "approved_amount":0.0,
    "pass_rate":0.0
  },
  {
    "time_hours":"11:00",
    "approved_amount":42706.0,
    "pass_rate":0.5
  },
  {
    "time_hours": "12:00",
    "approved_amount": null,
    "pass_rate": null,
  },
  {
    "time_hours":"13:00",
    "approved_amount":0.0,
    "pass_rate":0.0
  },
  {
    "time_hours": "14:00",
    "approved_amount": null,
    "pass_rate": null,
  },
  {
    "time_hours":"15:00",
    "approved_amount":37700.0,
    "pass_rate":0.5714285714
  },
  {
    "time_hours":"16:00",
    "approved_amount":0.0,
    "pass_rate":0.0
  },
  {
    "time_hours":"17:00",
    "approved_amount":29520.0,
    "pass_rate":0.6
  },
  {
    "time_hours":"18:00",
    "approved_amount":0.0,
    "pass_rate":0.0
  },{
    "time_hours":"19:00",
    "approved_amount":56216.0,
    "pass_rate":1.0
  },
  {
    "time_hours": "20:00",
    "approved_amount": null,
    "pass_rate": null,
  },
  {
    "time_hours": "21:00",
    "approved_amount": null,
    "pass_rate": null,
  },
  {
    "time_hours":"22:00",
    "approved_amount":5800.0,
    "pass_rate":1.0
  },
  {
    "time_hours":"23:00",
    "approved_amount":0.0,
    "pass_rate":0.0
  },
  {
    "time_hours": "00:00",
    "approved_amount": null,
    "pass_rate": null,
  },
]
这样的,请教一下大家这个要怎么做啊?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

じее 2022-09-14 16:36:41

不谢,拿走:

const beforeData = [{
  "time_hours": "09", // 时间
  "approved_amount": 28068.0,
  "pass_rate": 1.0
}];

const result = Array.from({length: 24}, (v, i) => i).map(hour => {
  let obj = beforeData.filter(has => Number(has.time_hours) === hour)[0] || {};
  return {
    time_hours: `${`00${hour}`.slice(-2)}:00`,
    approved_amount: obj.approved_amount || null,
    pass_rate: obj.pass_rate || null
  };
});

console.log(result);

就这么简单~~~

深海不蓝 2022-09-14 16:36:41

补充一个线性时间复杂度的

function getFullRange (beforeData) {
  return beforeData
    .reduce((arr, item) => {
      arr[+item.time_hours] = item
      return arr
    }, [...new Array(24)])
    .map((item, i) => {
      if (!item) {
        item = {
          time_hours: String(i),
          approved_amount: null,
          pass_rate: null,
        }
      }
      item.time_hours = item.time_hours.padStart(2, '0') + ':00'
      return item
    })
}
抽个烟儿 2022-09-14 16:36:41
var data = [{
  "time_hours": "09", // 时间
  "approved_amount": 28068.0,
  "pass_rate": 1.0
},
{
  "time_hours":"10",
  "approved_amount":0.0,
  "pass_rate":0.0
},
{
  "time_hours":"11",
  "approved_amount":42706.0,
  "pass_rate":0.5
},
{
  "time_hours":"13",
  "approved_amount":0.0,
  "pass_rate":0.0
},
{
  "time_hours":"15",
  "approved_amount":37700.0,
  "pass_rate":0.5714285714
},
{
  "time_hours":"16",
  "approved_amount":0.0,
  "pass_rate":0.0
},
{
  "time_hours":"17",
  "approved_amount":29520.0,
  "pass_rate":0.6
},
{
  "time_hours":"18",
  "approved_amount":0.0,
  "pass_rate":0.0
},{
  "time_hours":"19",
  "approved_amount":56216.0,
  "pass_rate":1.0
},
{
  "time_hours":"22",
  "approved_amount":5800.0,
  "pass_rate":1.0
},
{
  "time_hours":"23",
  "approved_amount":0.0,
  "pass_rate":0.0
}]

function trans (data) {
  let result = []
  let cur = 1
  function format(time) {
    time = time < 10 ? '0' + time : time
    return time + ':00'
  }
  function genRange(i, j) {
    let result = []
    for (; i < j; i++) {
      result.push({time_hours: format(i), approved_amount: null, pass_rate: null})
    }
    return result
  }
  data.forEach(item => {
    let now = +item.time_hours
    if (cur !== now) result = result.concat(genRange(cur, now))
    result.push(Object.assign({}, item, {time_hours: format(now)}))
    cur = now + 1
  })
  if (cur < 25) result = result.concat(genRange(cur, 25))
  return result
}
let result = trans(data)
止于盛夏 2022-09-14 16:36:41

Array.prototype.map

像你 2022-09-14 16:36:41
  let completeData = [{'time_hours': '01:00', 'approved_amount': null, 'pass_rate': null}, {'time_hours': '02:00', 'approved_amount': null, 'pass_rate': null}, {'time_hours': '03:00', 'approved_amount': null, 'pass_rate': null}, {'time_hours': '04:00', 'approved_amount': null, 'pass_rate': null}, {'time_hours': '05:00', 'approved_amount': null, 'pass_rate': null}, {'time_hours': '06:00', 'approved_amount': null, 'pass_rate': null}, {'time_hours': '07:00', 'approved_amount': null, 'pass_rate': null}, {'time_hours': '08:00', 'approved_amount': null, 'pass_rate': null}, {'time_hours': '09:00', 'approved_amount': null, 'pass_rate': null}, {'time_hours': '10:00', 'approved_amount': null, 'pass_rate': null}, {'time_hours': '11:00', 'approved_amount': null, 'pass_rate': null}, {'time_hours': '12:00', 'approved_amount': null, 'pass_rate': null}, {'time_hours': '13:00', 'approved_amount': null, 'pass_rate': null}, {'time_hours': '14:00', 'approved_amount': null, 'pass_rate': null}, {'time_hours': '15:00', 'approved_amount': null, 'pass_rate': null}, {'time_hours': '16:00', 'approved_amount': null, 'pass_rate': null}, {'time_hours': '17:00', 'approved_amount': null, 'pass_rate': null}, {'time_hours': '18:00', 'approved_amount': null, 'pass_rate': null}, {'time_hours': '19:00', 'approved_amount': null, 'pass_rate': null}, {'time_hours': '20:00', 'approved_amount': null, 'pass_rate': null}, {'time_hours': '21:00', 'approved_amount': null, 'pass_rate': null}, {'time_hours': '22:00', 'approved_amount': null, 'pass_rate': null}, {'time_hours': '23:00', 'approved_amount': null, 'pass_rate': null}, {'time_hours': '00:00', 'approved_amount': null, 'pass_rate': null}]
  let afterData = completeData.map(val => {
    let res = beforData.find(v => v.time_hours === val.time_hours.slice(0, 2))
    res ? res.time_hours = val.time_hours : res = val
    return res
  })
  console.log(afterData)

既然结果是固定的 就反过来遍历呗

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文