繁体   English   中英

在 JavaScript 中处理对象数组的数据 [重复]

Data massaging an array of Objects in JavaScript [duplicate]

提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供   中文繁体   英文版本   中英对照 版本,有任何建议请联系yoyou2525@163.com。

我有下面的数组。

Const array = [
  {
    "day": "Monday",
    "beginHour": "08:30:00",
    "endHour": "17:00:00"
  },
  {
    "day": "Tuesday",
    "beginHour": "08:30:00",
    "endHour": "17:00:00"
  },
  {
    "day": "Wednesday",
    "beginHour": "08:30:00",
    "endHour": "17:00:00"
  },
  {
    "day": "Thursday",
    "beginHour": "08:30:00",
    "endHour": "17:00:00"
  },
  {
    "day": "Friday",
    "beginHour": "08:30:00",
    "endHour": "17:00:00"
  }
]

我正在 NodeJs 中编写一个函数来处理这种数组,其中办公时间和日期可能会根据办公室而变化。 我试图返回低于值。

response = "Mon-Fri 8.30-5pm Closed Sat & Sun"

请建议/建议。 万分感激。

1 个回复

有一个解决方案....试试看:

 const array = [ { "day": "Monday", "beginHour": "08:30:00", "endHour": "17:00:00" }, { "day": "Tuesday", "beginHour": "08:30:00", "endHour": "17:00:00" }, { "day": "Wednesday", "beginHour": "08:30:00", "endHour": "17:00:00" }, { "day": "Thursday", "beginHour": "08:30:00", "endHour": "17:00:00" }, { "day": "Friday", "beginHour": "08:30:00", "endHour": "17:00:00" } ]; //create a function const days = arr => { //get days and time const [mon,fri] = [arr[0],arr[arr.length-1]]; //destructure const [dayM,openM,closeM] = Object.values(mon); const [dayF,openF,closeF] = Object.values(fri); return `${dayM}-${dayF} ${openM}-${closeF} Closed Sat & Sun.`; }; console.log(days(array));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题
 
粤ICP备18138465号  © 2020-2022 STACKOOM.COM