【问题标题】:How to take the value as array or convert it to array?如何将值作为数组或将其转换为数组?
【发布时间】:2021-06-21 10:16:19
【问题描述】:

My text area in HTML

I am getting this value from text field

But i want something like this. As an array

用户可以将对象数组粘贴到文本字段中,当他们单击按钮时,输出应返回唯一值。在这种情况下,它应该返回 1 和 2。当我在 JS 文件中单独执行时,我在获取输出方面没有问题。问题是我不知道如何从文本区域获取此值作为要在我的 JavaScript 文件中处理的对象数组。

【问题讨论】:

    标签: javascript html arrays


    【解决方案1】:

    您必须解析 JSON 字符串。在 JavaScript 中,您可以这样做:

    const obj = JSON.parse('{"name":"John", "age":30, "city":"New York"}');

    这将导致:

    {
       name:"John",
       age:30,
       city:"New York"
    }
    

    W3Schools JSON.Parse()

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 1970-01-01
      • 2010-11-04
      • 2020-08-21
      • 1970-01-01
      • 2011-06-08
      • 1970-01-01
      • 1970-01-01
      • 2022-08-02
      相关资源
      最近更新 更多