我有一個這樣的字串(HTML 選擇串列):
const config = {
"celsius": {
"celsius": v => v,
"fahrenheit": v => v * 1.8 32,
"kelvin": v => v 273.15,
"rankine": v => (v 273.15) * 1.8,
"reaumur": v => v * 0.8,
},
// more...
}
我想從輸出中洗掉前 5 個字符(operationPlace)
var listOperation = config[listFromV][listToV].substring(5);
document.getElementById("operationPlace").innerHTML = listOperation;
// now it looks like this: v => v or v => v * 1.8 32 etc.
// should look like this: v or v * 1.8 32 etc. (I'd like to change that "v" to numbers from the input (inputPlace) too)
我嘗試使用子字串,但它沒有用。有沒有辦法做到這一點?
uj5u.com熱心網友回復:
可以通過呼叫函式獲取字串形式的函式定義.toString,然后呼叫.substring去掉函式簽名部分:
var listOperation = config[listFromV][listToV];
document.getElementById("operationPlace").innerHTML = listOperation.toString().substring(5);
uj5u.com熱心網友回復:
將此字串轉換為陣列,然后提取所需的值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/487427.html
標籤:javascript 细绳 子串
