【发布时间】:2018-12-28 10:27:40
【问题描述】:
我如何在 JS 中遍历这样的结构,基本上它是一个包含对象数组的数组 这就是我在 console.log 时得到的结果
[Array(1)]
0: Array(1)
0: {day: "Friday", start: "2:00", end: "7:30"}
length: 1
__proto__: Array(0)
length: 1
__proto__: Array(0)
我试过了
formattedShifts.map(shift => shift.end)
但它失败了,formattedShifts 是我推入的数组 这是我创建数组的地方
let formattedShifts = [];
if(props.formData.isLinkedShifts) {
//converts shift.startTime and shift.endTime format
function toDays(startDateString, endDateString) {
const formatString = 'ddd MMM DD YYYY HH:mm:ss [GMT]ZZ';
const startDate = moment(startDateString, formatString);
const endDate = moment(endDateString, formatString);
const start = startDate.format('H:mm');
const end = endDate.format('H:mm');
const dates = [];
while(startDate.isSameOrBefore(endDate, 'day')) {
let currentDay = startDate.format('dddd');
dates.push({day: currentDay, start: start, end: end});
startDate.add(1, 'days');
}
return dates;
}
formattedShifts.push( toDays( props.formData.shifts.map( shift => shift.startTime),
props.formData.shifts.map( shift => shift.endTime)) );
}
【问题讨论】:
-
不要添加阵列的控制台日志。请添加实际数组
-
检查编辑,应该不错
标签: javascript arrays object