简体   繁体   English

Node Js根据unix时间戳排序Json对象

[英]Node Js sorting Json object based on the unix timestamp

I have the following json object:我有以下 json 对象:

var my_array = [
  {
    timestamp: '1571967208',
    transaction: 'DEPOSIT',
    name: 'Bob',
    amout: '10'
  },
  {
    timestamp: '1571967200',
    transaction: 'DEPOSIT',
    token: 'Roy',
    amount: '60'
  },
  {
    timestamp: '1571967189',
    transaction: 'WITHDRAWAL',
    token: 'Bob',
    amount: '10'
  },
  {
    timestamp: '1571966982',
    transaction: 'WITHDRAWAL',
    token: 'bob',
    amount: '50'
  }
]

I'm trying to sort this by timestamp (latest to last) and display new array but the timestamp in a human-readable format like 'Friday, October 25, 2019, 1:33:28 AM'.我正在尝试按时间戳(从最新到最后)对其进行排序并显示新数组,但时间戳以人类可读的格式显示,例如“2019 年 10 月 25 日,星期五,凌晨 1:33:28”。

I used the following code in my program:我在我的程序中使用了以下代码:

function comp(a, b) {
  return new Date(a.my_array .date).getTime() - new Date(b.my_array .date).getTime();
}

result.sort(comp);

But it's not sorting is it wrong or if any other way to do this?但这不是排序是错误的还是有其他方法可以做到这一点? For example new array like below:例如像下面这样的新数组:

var my_array = [
  {
    timestamp: 'Friday, October 25, 2019 1:33:28 AM',
    transaction: 'DEPOSIT',
    name: 'Bob',
    amout: '10'
  },
  ....
]

There are three problems with that code:该代码存在三个问题:

  1. You're using the date property, but the example uses the name timestamp .您使用的是date属性,但该示例使用名称timestamp

  2. The timestamp values are strings. timestamp值是字符串。 The Date constructor looks at the type of the argument you pass it and does different things for strings and numbers. Date构造函数查看您传递给它的参数的类型,并对字符串和数字执行不同的操作。 You want to convert those to number first (and multiply by 1000, to convert from seconds to milliseconds).你想先将它们转换为数字(然后乘以 1000,从秒转换为毫秒)。 (But there's no need for Date if you're just comparing them.) (但如果您只是比较它们,则不需要Date 。)

  3. You're using a.my_array.date and such in the callback, but what you want to use are the callbacks arguments directly.您在回调中使用a.my_array.date等,但您想要直接使用的是回调参数。

Since - will implicitly convert to number, the sorting part is:由于-将隐式转换为数字,因此排序部分为:

function comp(a, b) {
    return a.timetstamp - b.timestamp;
}

Then use map on the result to format the timestamp as a string (or just forEach or any other form of loop), converting it to date like this:然后在结果上使用maptimestamp格式化为字符串(或只是forEach或任何其他形式的循环),将其转换为日期,如下所示:

var dt = new Date(entry.timestamp * 1000);

It's * 1000 because yoru timestamps are in seconds, but new Date expects milliseconds.它是* 1000因为 yoru 时间戳以秒为单位,但new Date需要毫秒。 * will also implicitly convert to number. *也将隐式转换为数字。

Then convert timestamp to string however you like.然后根据需要将timestamp转换为字符串。

Here's an example using toString :下面是一个使用toString的例子:

my_array = my_array.sort(comp);
my_array.forEach(function(entry) {
    entry.timestamp = new Date(entry.timestamp * 1000).toString();
});

Live Example:现场示例:

 var my_array = [ { timestamp: '1571967208', transaction: 'DEPOSIT', name: 'Bob', amout: '10' }, { timestamp: '1571967200', transaction: 'DEPOSIT', token: 'Roy', amount: '60' }, { timestamp: '1571967189', transaction: 'WITHDRAWAL', token: 'Bob', amount: '10' }, { timestamp: '1571966982', transaction: 'WITHDRAWAL', token: 'bob', amount: '50' } ]; function comp(a, b) { return a.timetstamp - b.timestamp; } my_array = my_array.sort(comp); my_array.forEach(function(entry) { entry.timestamp = new Date(entry.timestamp * 1000).toString(); }); console.log(my_array);

Obviously, you'll have to adjust the string format to your desired one.显然,您必须将字符串格式调整为您想要的格式。 The only built-in formats are toString and toISOString , otherwise you have to roll your own or use a library like Moment.唯一的内置格式是toStringtoISOString ,否则你必须自己动手或使用像 Moment 这样的库。

You may try this你可以试试这个

result.sort(function(a,b){
  return ((new Date(a.timestamp).getTime()) - (new Date(b.timestamp).getTime()))
})

May be this is helpful for you. 可能对您有帮助。

 var my_array = [ { timestamp: '1571967208', transaction: 'DEPOSIT', name: 'Bob', amout: '10' }, { timestamp: '1571967200', transaction: 'DEPOSIT', token: 'Roy', amount: '60' }, { timestamp: '1571967189', transaction: 'WITHDRAWAL', token: 'Bob', amount: '10' }, { timestamp: '1571966982', transaction: 'WITHDRAWAL', token: 'bob', amount: '50' } ] function comp(a, b) { return new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime(); } let newarr = my_array.sort(comp); let x = newarr.map((data)=>{ let p = data; p.timestamp = new Date(parseInt(data.timestamp*1000)).toString(); return p; }); console.log(x) 

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

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