d3.js - 结合数据获取和数组迭代
问题描述
刚尝试优化一些代码,发现示例2中的例程不起作用,只返回最后一项!为什么不更新 obj forEach 的任何想法?
示例 1
const spreadsheet = await d3.tsv(url).then(data => data.forEach(function (d) {
let names =[];
return names.push({"code": d.Code, "date": d.Date})
}))
return names;
示例 2
let obj = {};
const spreadsheet = await d3.tsv(url).then(data => data.forEach(function (d) {
if (!obj[d.code]) obj[d.code] = {code: d.code};
obj[d.code]['date'] = d.Date;
})
return Object.values(obj)
}
解决方案
The bug is happening due to a misconception in the use of await and .then().
There is no need to await a promise if you're handling it with .then(). The await keyword exists to avoid the use of .then().
Reestructuring your code to remove the .then(), we have:
let obj = {};
const spreadsheet = await d3.tsv(url);
spreadsheet.forEach(function (d) {
if (!obj[d.code]) obj[d.code] = { code: d.code };
obj[d.code]["date"] = d.Date;
});
console.log(obj); // Object here
However, if you're not inside an async function, you can not use await. If that is the case, this means you'll have to stick with .then(), and the data can not be accessed outside the callback. You would have:
d3.tsv(url).then((spreadsheet) => {
let obj = {};
spreadsheet.forEach(function (d) {
if (!obj[d.code]) obj[d.code] = { code: d.code };
obj[d.code]["date"] = d.Date;
});
console.log(obj); // Object here
/* Everything that uses obj must be called inside the .then() callback*/
});
In both approaches above, the obj variable is a JSON where the key is the code, and the value is the whole object with the code, which I think is the optimization you're looking for. Using Object.values(obj) would return just an array, exactly like your example 1.
推荐阅读
- xcode - Xcode 11 beta 4:SwiftUI 预览和构建阶段 agvtool 不兼容?
- javascript - 如何以反应形式存储布尔值?
- c++ - 相关 C++ 类中多态性的最佳实践?
- c# - 我可以将 Visual Studio 配置为允许调试步骤进入 NuGet 包吗?
- typescript - 如何为具有子级和父级的实体创建域模型
- java - 从 Excel 中读取表格数据并显示 Java 中唯一组合的列数
- python - 在 csv 文件中查找字符串时提取一行
- c++ - 如果 T 不是整数,如何禁用非空 std::set 作为函数参数?
- java - 仅在特定片段上的 Android 导航组件侧导航
- java - HttpUrlConnect 不作为线程的一部分发送发布请求