将数组转换为字符串到数组javascript

时间:2019-03-07 14:39:49

标签: javascript

这可能是一个愚蠢的问题,但我有这样的字符串:

.search-table table tbody tr:hover {
    box-shadow: inset 0px 0px 3px 0px #00000082;
}

我想将其转换为实际数组:

from discord import Member
from discord.ext.commands import Bot, has_permissions, CheckFailure, BadArgument

bot = Bot("!")

@bot.command(pass_context=True, name="kick")
@has_permissions(kick_members=True)
async def kick_command(ctx, *, target: Member):
    if target.server_permissions.administrator:
        await bot.say("Target is an admin")
    else:
        try:
            await bot.kick(target)
            await bot.say("Kicked")
        except Exception:
            await bot.say("Something went wrong")

@kick_command.error
async def kick_error(error, ctx):
    if isinstance(error, CheckFailure):
         await bot.send_message(ctx.message.channel, "You do not have permissions")
    elif isinstance(error, BadArgument):
        await bot.send_message(ctx.message.channel, "Could not identify target")
    else:
        raise error

我该怎么办?

4 个答案:

答案 0 :(得分:3)

只需JSON解析它! JSON.parse("[1, 2, 3, 4]")

答案 1 :(得分:0)

var string = "[1, 2, 3, 4]";
JSON.parse(string);

答案 2 :(得分:0)

您应该使用并验证Tom Finney或Ashishya11或Hasee Amarathunga的答案,因为如果您的字符串输入来自不受信任的来源,则我的安全漏洞就很严重。但是无论如何,您也可以这样:

const array = eval('[1, 2, 3, 4]')

答案 3 :(得分:0)

尝试一下:

JSON.parse("[1, 2, 3, 4]")