स्ट्रिंग प्रकार मान को सरणी प्रकार में बदलने के लिए, पार्स () विधि का उपयोग करें। निम्नलिखित कोड है -
उदाहरण
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.js
The 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' }
]