如何从此字符串中删除前5个字符?

发布于 2025-02-05 11:33:08 字数 714 浏览 1 评论 0原文

我有一个类似的字符串(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...
}

我想

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)

从尝试使用substring的输出(操作场所)中剪切前5个字符,但它不起作用。有办法这样做吗?

I have a string like this (HTML select list):

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...
}

And I want to cut the first 5 characters from the output (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)

I tried using substring, but it didn't work. Is there a way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抚笙 2025-02-12 11:33:08

您可以通过在其上调用.toString来获取字符串形式的函数的定义,然后调用.substring以摆脱功能签名部分:

var listOperation = config[listFromV][listToV];
document.getElementById("operationPlace").innerHTML = listOperation.toString().substring(5);

请注意这是幼稚的方法,只能适用于签名的签名,由单个1个字符的长论点和箭头符号组成,而没有括号。

You can obtain the definition of a function in the form of a string by calling .toString on it, then call .substring to get rid of the function signature part:

var listOperation = config[listFromV][listToV];
document.getElementById("operationPlace").innerHTML = listOperation.toString().substring(5);

Note that this is approach is naive and will only work for functions whose signature is composed of a single 1-character long argument and arrow notation without parenthesis.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文