【发布时间】:2020-09-29 08:17:39
【问题描述】:
我正在构建一个反应应用程序来显示来自 pandascore api 的电子竞技比赛。我已经提取了所有比赛的日期,并创建了一个多层数组,将它们按周分组。
我正在尝试用相应的匹配替换数组中的日期。这样,我可以拥有一个按周分组的对象。
// map an array of dates
//getting dates that we will replace
// ie ["2018-07-29T20:41:28Z", "2018-07-29T21:32:14Z", "2018-08-05T01:08:11Z"]
Object.values(groups).map((matchDateTime) => {
console.log(matchDateTime);
//loop over every date in the array
for (let d = 0; d < matchDateTime.length; d++) {
//set a variable and set it to find a match from the matches array
//that has the same date and time to the
//entry in the matchDateTime array
let matchWithDate = matches.reduce( match => {
//if dates match, then we will replace the date in the
//matchDateTime array to the match object we found
//this is the logic I'm looking for but doesn't return anything
match.begin_at = matchDateTime[d] ? matchDateTime[d] = match : 'no match';
//returns the correct date - line 71 in the screenshot
console.log(matchDateTime[d]);
//returns the correct match - line 72
console.log(match);
// returns the coresponding match date/time - line 73
console.log(match.begin_at);
}
);
}
// console.log(groups);
});
循环运行几次后出现错误。不应该有任何日期/时间为空或未定义,因为我直接从我正在检查的对象中提取它们。
我觉得我很接近。任何帮助表示赞赏!
【问题讨论】:
-
您可能应该重新查看array::reduce。它的回调的第一个参数是一些“累积”值(你也永远不会在回调中返回)你正在减少一个数组,第二个参数是当前元素数组被迭代。您对
array::map的使用也存在问题。 -
使用
forEach而不是reduce
标签: javascript arrays reactjs javascript-objects