我從 api 得到一系列回應。下面是回應陣列。
[{createdBy:"user1",updatedDttm:
"2022-01-20T07:31:35.544Z"},
createdBy:"user2", updatedDttm:
"2022-02-20T09:31:37.544Z"}]
從上面的回應中,我想為每個用戶拆分“updatedDttm”(日期和時間)并將其保存到與“日期”、“時間”相同的陣列中,如下所示。
[{createdBy:"user1",date:
"2022-01-20", time:"07:31:35"},
createdBy:"user2", date:
"2022-02-20", time: "09:31:37"}]
我正在使用 Angular.js。
uj5u.com熱心網友回復:
請在下面找到一個可能的解決方案。
const response = [{createdBy:"user1",updatedDttm:'2022-01-20T07:31:35.544Z'},
{createdBy:"user2", updatedDttm: '2022-02-20T09:31:37.544Z'}].map(x => ({
createdBy: x.createdBy,
date: new Date(x.updatedDttm).toLocaleDateString(),
time: new Date(x.updatedDttm).toLocaleTimeString(),
}));
console.log(response);
uj5u.com熱心網友回復:
對于它的價值,不是最優雅的解決方案,但如果您的物件中的資料是一致的,您可以嘗試:
const a = [
{
"createdBy": "user1",
"updatedDttm": "2022-01-20T07:31:35.544Z"
},
{
"createdBy": "user2",
"updatedDttm": "2022-02-20T09:31:37.544Z"
}
];
const response = a.map(x => ({
createdBy: x.createdBy,
date: x['updatedDttm'].split('Z')[0].split('T')[0],
time: x['updatedDttm'].split('Z')[0].split('T')[1].split('.')[0],
}));
console.log(response);
使用 momentjs 和/或 lodash 可以實作更優雅的解決方案;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/428403.html
