【发布时间】:2020-12-15 20:28:57
【问题描述】:
这是 FileObject 的构建方式,我需要帮助来确定如何根据提供的日期进行迭代和查找值。
interface MyProps{
date: string;
cDataFileName?: string | undefined;
iDataFileName?: string | undefined;
}
在开发工具中,它会像这样显示..
FileObject: Array(4)
0: {date: "8/26/2020", cDataFileName: : "small_range_c-compressed.json", influenzaDataFileName: "mall_range_f-compressed.jso"}
1: {date: "8-24-2020", cDataFileName: : "small_range_c-compressed.json", iDataFileName: : "mall_range_f-compressed.jso"}
2: {date: "8-13-2020", cDataFileName: : "small_range_c-compressed.json", iDataFileName: : "mall_range_f-compressed.jso"}
3: {date: "7-15-2020", cDataFileName: : "small_range_c-compressed.json", iDataFileName: : "mall_range_f-compressed.jso"}
length: 4
我需要做的是查找 FileObject 中是否存在日期为“2020 年 8 月 26 日”的数据
并为其获取 cDataFileName 和 IDataFileName 值。
const today = "8/26/2020";
console.log("testing" + FileObject [today as any]); // Its coming as testingUndefined.
if (today in FileObject ) {
console.log("today" + today); // Not even going there
}
请帮助我找出一种最佳方法来遍历此对象数组以根据提供的日期“2020 年 8 月 26 日”获取。
-----------更新1---------- 这就是我将值添加到 FileObject 的方式。
const FileObject: MyProps[] = [];
FileObject.push({
date: file,
cDataFileName: MycovidFileName,
iDataFileName: MyinDataFileName,
});
【问题讨论】:
-
FileObject.filter(x => x.date === today) 如果有多个对象具有相同的日期或 .find() 如果只有一个
-
感谢 Ihorbond,现在就试试。我如何才能访问与今天日期匹配的对象?
-
我收到此消息 TypeError: FileObject.filter is not a function.. 我需要为它导入任何库吗?
-
看来 FileObject 不是一个数组。如果您在调用 .filter() 的行之前进行此检查,console.log(Array.isArray(FileObject)) 会产生什么?
-
所以这个语句console.log(Array.isArray(FileObject));说的是 false 和 console.log(FileObject);显示了我在上面的帖子中从开发工具中放入的结果。其中包含 4 个对象。
标签: javascript reactjs typescript react-native