如何在JavaScript中将字符串类型的值转换为数组类型?
要将字符串类型的值转换为数组类型,请使用parse()方法。以下是代码-
示例
var customerDetails='[{"name": "John", "countryName": "US"},
{"name": "David", "countryName": "AUS"},
{"name": "Bob", "countryName": "UK"}
]';
console.log("The actual value="+customerDetails);
var convertStringToArray=JSON.parse(customerDetails);
console.log("After converting string to array objects=");
console.log(convertStringToArray);
要运行上述程序,您需要使用以下命令-
node fileName.js.
输出结果
在这里,我的文件名为demo123.js。这将产生以下输出-
PS C:\Users\Amit\JavaScript-code> node demo123.jsThe actual value=[{"name": "John", "countryName": "US"}, {"name": "David", "countryName":
"AUS"}, {"name": "Bob", "countryName": "UK"}]
After converting string to array objects=[
{ name: 'John', countryName: 'US' },
{ name: 'David', countryName: 'AUS' },
{ name: 'Bob', countryName: 'UK' }
]
以上是 如何在JavaScript中将字符串类型的值转换为数组类型? 的全部内容, 来源链接: utcz.com/z/322473.html