gpt4 book ai didi

javascript - 在 Javascript/React 中格式化设备 ID 和日期

转载 作者:行者123 更新时间:2023-12-02 22:11:51 26 4
gpt4 key购买 nike

我从后端收到以下 JSON 响应:

{
"assetStatus": "active",
"auditLogs": {
"20191115T123426": {
"authorizedBy": "admin",
"log": "Config update for E5:29:C7:E2:B7:64 (fieldsight-octo-d82d3224-4c11-4b7b-ae18-36eac742710e) in iotCore with '{\"selftest\":0}' at 2019-11-15T12:34:26"
},
"20191115T123427": {
"authorizedBy": "admin",
"log": "Config update for E5:29:C7:E2:B7:64 (fieldsight-octo-d82d3224-4c11-4b7b-ae18-36eac742710e) in iotCore with '{\"selftest\":0}' at 2019-11-15T12:34:27"
},
"20191115T123428": {
"authorizedBy": "admin",
"log": "Config update for E5:29:C7:E2:B7:64 (fieldsight-octo-d82d3224-4c11-4b7b-ae18-36eac742710e) in iotCore with '{\"selftest\":0}' at 2019-11-15T12:34:28"
},
"20191115T123430": {
"authorizedBy": "admin",
"log": "Config update for E5:29:C7:E2:B7:64 (fieldsight-octo-d82d3224-4c11-4b7b-ae18-36eac742710e) in iotCore with '{\"selftest\":0}' at 2019-11-15T12:34:30"
},
"20191115T142340": {
"authorizedBy": "admin",
"log": "Config update for E5:29:C7:E2:B7:64 (fieldsight-octo-d82d3224-4c11-4b7b-ae18-36eac742710e) in iotCore with '{\"report\":\"\"}' at 2019-11-15T14:23:40"
},
"20191115T142402": {
"authorizedBy": "admin",
"log": "Config update for E5:29:C7:E2:B7:64 (fieldsight-octo-d82d3224-4c11-4b7b-ae18-36eac742710e) in iotCore with '{\"report\":\"\"}' at 2019-11-15T14:24:02"
}
},
"color": "red",
"company": "Wizense Oy",
"firmware": 0.19,
"hardware": "B 2.0",
"mac": "E5:29:C7:E2:B7:64",
"mechanic": 3,
"process": {
"status": "OK"
},
"serialNumber": "HSKJRI891",
"type": "wear"
}

我的代码:


data = () => {
let now = moment();
console.log(`HERE DATE : ${now}`);
const device_info_path = `/tenants/${tenantId}/assets/infrastructure/${this.props.macId}/deviceInfo`;
const itemsRef = database.ref(device_info_path);
itemsRef.on("value", snapshot => {
if (snapshot.val().auditLogs == null) {
console.log("00000 EMPTY LOGS");
this.setState({
no_data: "Empty"
});
} else {
let keys_in_audit_logs = Object.keys(snapshot.val().auditLogs);
let logs = [];
keys_in_audit_logs.forEach(date => {
logs.push({
user: snapshot.val().auditLogs[date].authorizedBy,
action: snapshot
.val()
.auditLogs[date].log.replace(/[{("':'")}]/g, ""),
date: date.split("T").join("-")
});
});
this.setState({
user_actions: logs
});
}
});
};

这会生成日志,其中包含以下条目:

{
"date": "20191115-142402"
"log": "Config update for
E529C7E2B764, fieldsight-octo-d82d3224-4c11-4b7b-ae18-36eac742710e
in iotCore with report at 2019-11-15T142402"
"authorizedBy": "Admin"
}

如何将设备 ID 格式化为 E5:29:C7:E2:B7:64 并将日期格式化为 2019:11:15-14:24:02 或任何其他可读格式?我可以使用 MomentJS console.log 格式化日期,但它不会向用户显示格式化。

最佳答案

对于设备 ID,您可以拥有:

let deviceID = "E529C7E2B764"
// Here, a regular expression it's used, and it puts a ':' character
// after each 2 chars. The result is: "E5:29:C7:E2:B7:64:". The last ":"
// should be removed, and this is the reason to use .slice method.
deviceID = deviceID.replace(/(.{2})/g,"$1:").slice(0, -1)

输出:E5:29:C7:E2:B7:64

对于日期 - 类似的东西。

一个简单的方法是:

// The split method splits the string by '-' character. The return is an array of two strings in this case:  "20191115" and "142402".
let dateArray = "20191115-142402".split("-")
let date = dateArray[0]
let time = dateArray[1]


// Since the regular expression also puts the ":" character after each 2 chars,
// we have to remove the first one because of the year format.
// Before calling .replace(":", "") -> 20:19:11:15
// and after -> 2019:11:15
// The slice method is used here also to remove the last ":".

date = date.replace(/(.{2})/g,"$1:").slice(0, -1).replace(":", "")
time = time.replace(/(.{2})/g,"$1:").slice(0, -1)

console.log(date + "-" + time)

输出:2019:11:15-14:24:02

更新:添加评论。

关于javascript - 在 Javascript/React 中格式化设备 ID 和日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59532993/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com