我有以下文字:
LOG: time="2021-12-09T16:55:25 01:00" level=debug msg="?? GetLatestBlock called" blockHeight=3 blockID=ee98016d268630db54b814d18d0127edac8cc36f90d193c0c6f5fd4909bbd8b1
,LOG: time="2021-12-09T16:55:25 01:00" level=debug msg="?? GetAccountAtLatestBlock called" address=f8d6e0586b0a20c7
,LOG: time="2021-12-09T16:55:26 01:00" level=debug msg="??? Transaction submitted" txID=5a46897e60f13ee68e11ef754983fefe9ec7bc8a2ca6079f0665e404138735e9
,LOG: time="2021-12-09T16:55:26 01:00" level=info msg="? Transaction executed" computationUsed=6 txID=5a46897e60f13ee68e11ef754983fefe9ec7bc8a2ca6079f0665e404138735e9
,LOG: time="2021-12-09T16:55:26 01:00" level=debug msg="\x1b[1;34mLOG\x1b[0m \x1b[2m[5a4689]\x1b[0m \"Hello from Emulator\""
time="2021-12-09T16:55:26 01:00" level=debug msg="\x1b[1;34mLOG\x1b[0m \x1b[2m[5a4689]\x1b[0m 0x1cf0e2f2f715450"
time="2021-12-09T16:55:26 01:00" level=debug msg="\x1b[1;34mLOG\x1b[0m \x1b[2m[5a4689]\x1b[0m 0x179b6b1cb6755e31"
time="2021-12-09T16:55:26 01:00" level=debug msg="?? Block #4 committed" blockHeight=4 blockID=5fd780a98baad6d30f66cf75e76c3e1c9398097a9bb2e3f239f0cd7e166f6932
,LOG: time="2021-12-09T16:55:26 01:00" level=debug msg="?? GetTransactionResult called" txID=5a46897e60f13ee68e11ef754983fefe9ec7bc8a2ca6079f0665e404138735e9
存盤在一個名為transactionLogs(的變數中,因此您看到的輸出是console.log(transactionLogs)),它是一個字串。
我想檢查事務日志是否包含兩個地址。0x01cf0e2f2f715450一份來自Alice ,一份來自 Bob 0x179b6b1cb6755e31。
我的問題是,當我這樣呼叫時includes(其中Alice和Bob是字串):
Alice; // 0x01cf0e2f2f715450
Bob; // 0x179b6b1cb6755e31
transactionLogs.includes(Alice); // false
transactionLogs.includes(Bob); // true
對 Alice 的搜索回傳false。
我認為它與轉義反斜杠有關,所以我嘗試運行,transactionLogs.replace(String.fromCharCode(92), '')但這不會改變結果。
這里發生了什么?為什么includes不回來true找愛麗絲?
uj5u.com熱心網友回復:
為什么包括不
true為愛麗絲回傳?
因為該字串不存在于日志字串中:
const transactionLogs = `
LOG: time="2021-12-09T16:55:25 01:00" level=debug msg="?? GetLatestBlock called" blockHeight=3 blockID=ee98016d268630db54b814d18d0127edac8cc36f90d193c0c6f5fd4909bbd8b1
,LOG: time="2021-12-09T16:55:25 01:00" level=debug msg="?? GetAccountAtLatestBlock called" address=f8d6e0586b0a20c7
,LOG: time="2021-12-09T16:55:26 01:00" level=debug msg="??? Transaction submitted" txID=5a46897e60f13ee68e11ef754983fefe9ec7bc8a2ca6079f0665e404138735e9
,LOG: time="2021-12-09T16:55:26 01:00" level=info msg="? Transaction executed" computationUsed=6 txID=5a46897e60f13ee68e11ef754983fefe9ec7bc8a2ca6079f0665e404138735e9
,LOG: time="2021-12-09T16:55:26 01:00" level=debug msg="\x1b[1;34mLOG\x1b[0m \x1b[2m[5a4689]\x1b[0m \"Hello from Emulator\""
time="2021-12-09T16:55:26 01:00" level=debug msg="\x1b[1;34mLOG\x1b[0m \x1b[2m[5a4689]\x1b[0m 0x1cf0e2f2f715450"
time="2021-12-09T16:55:26 01:00" level=debug msg="\x1b[1;34mLOG\x1b[0m \x1b[2m[5a4689]\x1b[0m 0x179b6b1cb6755e31"
time="2021-12-09T16:55:26 01:00" level=debug msg="?? Block #4 committed" blockHeight=4 blockID=5fd780a98baad6d30f66cf75e76c3e1c9398097a9bb2e3f239f0cd7e166f6932
,LOG: time="2021-12-09T16:55:26 01:00" level=debug msg="?? GetTransactionResult called" txID=5a46897e60f13ee68e11ef754983fefe9ec7bc8a2ca6079f0665e404138735e9
`;
const Alice = `0x01cf0e2f2f715450`;
const Bob = `0x179b6b1cb6755e31`;
console.log({
Alice: transactionLogs.includes(Alice),
Bob: transactionLogs.includes(Bob),
});
uj5u.com熱心網友回復:
您似乎正在搜索事務日志中列印錯誤的十六進制地址。
我認為您應該更改事務日志邏輯,以避免在登錄時切掉地址中的前導 0。
否則,您必須從要搜索的地址中洗掉前導零,即
transactionLog.includes('0x' parseInt(Alice, 16).toString(16))
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/377963.html
標籤:javascript 数组 打字稿
下一篇:從鍵不在陣列中的物件中洗掉條目
