javascript - 从对象属性获取日期值 - javascript
问题描述
我怎样才能只从下面的对象中获取没有字符串值的日期?
0: {2020-09-02: "string_1", 2020-09-03: "string_2"}
1: {2020-09-01: "string_1", 2020-09-05: "string_2"}
我的目标是只获取日期并将它们分组到一个数组中。
预期结果:[2020-09-02, 2020-09-03, 2020-09-01, 2020-09-05]
到目前为止我尝试的是使用Object.getOwnPropertyNames:
console.log('property name: ', Object.getOwnPropertyNames(getDateProperties)) // return ["0","1"]
这有可能实现吗?
解决方案
使用 reduce 和 Object 键从对象中获取日期
const list = [{
"2020-09-02": "string_1",
"2020-09-03": "string_2"
},
{
"2020-09-01": "string_1",
"2020-09-05": "string_2"
}
]
const result = list.reduce((acc, x) => {
const keys = Object.keys(x)
acc = [...acc, ...keys]
return acc;
}, [])
console.log(result)
推荐阅读
- actionscript-3 - 在 Adobe Animate CC (AS3) 中滚动和滑动
- uwp - 如何从用 C# 编写的 UWP 应用调用 C# 可选包中的代码
- javascript - 如何使用 javascript 检查选择器类中的选项数量?
- discord - 嵌入动态帮助命令的内容
- google-apps-script - 在列中搜索值并覆盖该行,否则添加新行
- php - 在“foreach”构造中键入检查键或值的最佳方法?
- leaflet - 生成已打印自定义数据的地图图块
- python - 如何在一个循环下有效地连接两个 Pandas 数据框?
- postgresql - 计算多个记录组的百分比
- node.js - 如何在谷歌存储上创建 Zip 并将文件上传到它?