我想从一个变量中移除双引号。

huangapple go评论67阅读模式
英文:

I want to remove double quotes from a variable

问题

var aProd = "{'name':'Product One','description':'Description Product One','unit_amount':{'currency_code':'USD','value':'247','sku':'h545'},'quantity':'1'}";

var item = new Array(aProd);
console.log(item);

将上述代码中的双引号替换为单引号后的结果:

[
    {'name':'Product One','description':'Description Product One','unit_amount':{'currency_code':'USD','value':'247','sku':'h545'},'quantity':'1'}
]
英文:
var aProd = "{'name':'Product One','description':'Description Product One','unit_amount':{'currency_code':'USD','value':'247','sku':'h545'},'quantity':'1'}";


var item = new Array(aProd);
  console.log(item);

result this

[
    "{'name':'Product One','description':'Description Product One','unit_amount':{'currency_code':'USD','value':'247','sku':'h545'},'quantity':'1'}"
]

How remove double quotes?

to this

[
    {'name':'Product One','description':'Description Product One','unit_amount':{'currency_code':'USD','value':'247','sku':'h545'},'quantity':'1'}
]

already tried

var item = new Array(String(ci).replace(/"/g, ""));

or

var item = ci.toString().replace(/"/g, "");

but I can't remove double quotes

答案1

得分: 2

使用JSON.parse(在将所有单引号转换为双引号后):

// 开始代码片段:js 隐藏:false 控制台:true babel:false

// 语言:lang-js

let aProd = '{"name":"Product One","description":"Description Product One","unit_amount":{"currency_code":"USD","value":"247","sku":"h545"},"quantity":"1"}';
let res = [JSON.parse(aProd)];
console.log(res);

// 结束代码片段
英文:

Use JSON.parse (after converting all single quotes to double quotes):

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let aProd = &quot;{&#39;name&#39;:&#39;Product One&#39;,&#39;description&#39;:&#39;Description Product One&#39;,&#39;unit_amount&#39;:{&#39;currency_code&#39;:&#39;USD&#39;,&#39;value&#39;:&#39;247&#39;,&#39;sku&#39;:&#39;h545&#39;},&#39;quantity&#39;:&#39;1&#39;}&quot;;
let res = [JSON.parse(aProd.replaceAll(&quot;&#39;&quot;, &#39;&quot;&#39;))];
console.log(res);

<!-- end snippet -->

答案2

得分: 0

这似乎是您想要创建一个数组,其中该数据是其第一个对象。现在,数据不是有效的JSON,因此您需要首先将所有单引号替换为双引号,然后解析它,并将其包装在[]括号中。

const aProd = "{\"name\":\"Product One\",\"description\":\"Description Product One\",\"unit_amount\":{\"currency_code\":\"USD\",\"value\":\"247\",\"sku\":\"h545\"},\"quantity\":\"1\"}";

// 用双引号替换单引号
const json = aProd.replaceAll("'", "\"");

// 解析现在有效的JSON,并将其放入一个数组中
const arr = [JSON.parse(json)];

// 这是您的新数据结构
console.log(arr);

// 并在这里,作为示例,我们记录来自该数组的第一个`[0]`元素的对象的`name`值
console.log(arr[0].name);

希望这能帮助您。

英文:

It sounds like you want to create an array with that data as its first object. Now, the data isn't valid JSON so you need to replace all the single quotes with double quotes first, parse it, and then wrap it in [] braces.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const aProd = &quot;{&#39;name&#39;:&#39;Product One&#39;,&#39;description&#39;:&#39;Description Product One&#39;,&#39;unit_amount&#39;:{&#39;currency_code&#39;:&#39;USD&#39;,&#39;value&#39;:&#39;247&#39;,&#39;sku&#39;:&#39;h545&#39;},&#39;quantity&#39;:&#39;1&#39;}&quot;;

// Replace the single quotes with double quotes
const json = aProd.replaceAll(&quot;&#39;&quot;, &#39;&quot;&#39;);

// Parse the now-valid JSON, and place it in an array
const arr = [JSON.parse(json)];

// Here&#39;s your new data structure
console.log(arr);

// And here, and an example, we log the value
// of `name` from the object which is the
// first `[0]` element of the array
console.log(arr[0].name);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月19日 09:10:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497400.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定