简体   繁体   English

同一 object 上的不同类型结果

[英]Different typeof result on the same object

I am getting some data from S3 using AWS SDK.我正在使用 AWS SDK 从 S3 获取一些数据。 The response I am getting is this:我得到的回应是这样的:

{
...
,
    "Contents": [
        {
            "Key": "data copy.csv",
            "LastModified": "2021-02-19T11:16:24.000Z",
            "ETag": "\"69d6972deead4b9a378473654c878d75\"",
            "Size": 305,
            "StorageClass": "STANDARD"
        },
        {
            "Key": "data.csv",
            "LastModified": "2021-02-19T11:16:24.000Z",
            "ETag": "\"69d6972deead4b9a378473654c878d75\"",
            "Size": 305,
            "StorageClass": "STANDARD"
        },
        {
            "Key": "folder1/",
            "LastModified": "2021-02-19T11:16:12.000Z",
            "ETag": "\"d41d8cd98f00b204e9800998ecf8427e\"",
            "Size": 0,
            "StorageClass": "STANDARD"
        },
        {
            "Key": "folder2/",
            "LastModified": "2021-02-19T11:16:16.000Z",
            "ETag": "\"d41d8cd98f00b204e9800998ecf8427e\"",
            "Size": 0,
            "StorageClass": "STANDARD"
        }
    ],
...
}

When I loop through the properties of one of the Contents items using this code, I can see that type of each of the items is "string":当我使用此代码遍历内容项之一的属性时,我可以看到每个项的类型都是“字符串”:

for (const property in Contents[0]) {
   console.log(`Type of ${property}: ${typeof property}`);
}

outputs:输出:

Type of Key: string
Type of LastModified: string
Type of ETag: string
Type of Size: string
Type of StorageClass: string

I want to sort the items by their date - "LastModified".我想按日期对项目进行排序 - “LastModified”。 The problem is that when I ask for the type of "LastModified", the output is "object".问题是当我询问“LastModified”的类型时,output 是“对象”。

console.log(typeof Contents[0].LastModified); // object
console.log(typeof Contents[0]['LastModified']); // object

Since it's an object, I cannot compare using String comparison.由于它是 object,我无法使用字符串比较进行比较。 So my questions are:所以我的问题是:

  1. Why does one method say it's a "string" and another one says it's an "object"?为什么一种方法说它是“字符串”而另一种方法说它是“对象”?
  2. What is the best way to convert it to a string (or a date) so I can compare the items?将其转换为字符串(或日期)以便我可以比较项目的最佳方法是什么?
for (const property in Contents[0]) {
   console.log(`Type of ${property}: ${typeof property}`);
}

The property key is a string.属性是一个字符串。 You're testing typeof of the property key, not the property value.您正在测试属性键的 typeof,而不是属性值。

Typeof the property value would be属性值的类型将是

for (const property in Contents[0]) {
   console.log(`Type of ${property}: ${typeof Contents[0][property]}`);
}
  1. It most likely has already been converted to a native Date object by the AWS SDK.它很可能已被 AWS SDK 转换为原生 Date object。 For comparison, use Contents[0].LastModified.getTime() to return the UNIX epoch timestamp (in milliseconds).为了比较,使用Contents[0].LastModified.getTime()返回 UNIX 纪元时间戳(以毫秒为单位)。

Note that if your dates are in string format, you first need to parse them to Date请注意,如果您的日期是字符串格式,您首先需要将它们解析为 Date

sort an object array by date property?按日期属性对 object 数组进行排序?

array.sort(function(a,b){
// Turn your strings into dates, and then subtract them
// to get a value that is either negative, positive, or zero.
return new Date(b.date) - new Date(a.date);
});

For your code array will be replaced with Contents[0]对于您的代码数组将替换为Contents[0]

Each item in Contents array is an object of key-value pairs. Contents数组中的每一项都是键值对的 object。 First method you check typeof the “key” (the property in your code), while second method you check against the “value”.第一种方法typeof “键”(代码中的property )的类型,而第二种方法检查“值”。 No wonder they say different things.难怪他们会说不同的话。

AWS SDK deserializes/revives those content items into native JS object. AWS SDK 将这些内容项反序列化/恢复为原生 JS object。 It turn the string representation of date into a JS Date instance, that why typeof check says "object".它将日期的字符串表示形式转换为 JS Date实例,这就是typeof检查说“对象”的原因。 If you check instanceof Contents[0].LastModified === Date it will say true .如果您检查instanceof Contents[0].LastModified === Date它会说true

To sort items by date, best practice is to convert date value into number, value of which is integer that represents unix timestamp in milliseconds.要按日期对项目进行排序,最佳做法是将日期值转换为数字,其值为 integer,表示 unix 时间戳,以毫秒为单位。 Example:例子:

Contents.sort((a, b) => Number(a.LastModified) - Number(b.LastModified))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM