英文:
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 = "{'name':'Product One','description':'Description Product One','unit_amount':{'currency_code':'USD','value':'247','sku':'h545'},'quantity':'1'}";
let res = [JSON.parse(aProd.replaceAll("'", '"'))];
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 = "{'name':'Product One','description':'Description Product One','unit_amount':{'currency_code':'USD','value':'247','sku':'h545'},'quantity':'1'}";
// Replace the single quotes with double quotes
const json = aProd.replaceAll("'", '"');
// Parse the now-valid JSON, and place it in an array
const arr = [JSON.parse(json)];
// Here'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 -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论