मान लें कि निम्नलिखित हमारी JSON सरणी है -
var details = [
{
"customerDetails": [
{
"customerName": "John Smith",
"customerCountryName": "US"
}
]
},
{
"customerDetails": [
{
"customerName": "David Miller",
"customerCountryName": "AUS"
}
]
},
{
"customerDetails": [
{
"customerName": "Bob Taylor",
"customerCountryName": "UK"
}
]
}
] केवल CustomerName मान प्राप्त करने के लिए, मानचित्र की अवधारणा का उपयोग करें () -
उदाहरण
var details = [
{
"customerDetails": [
{
"customerName": "John Smith",
"customerCountryName": "US"
}
]
},
{
"customerDetails": [
{
"customerName": "David Miller",
"customerCountryName": "AUS"
}
]
},
{
"customerDetails": [
{
"customerName": "Bob Taylor",
"customerCountryName": "UK"
}
]
}
]
var allCustomerName = details.map(obj=>
obj.customerDetails[0].customerName);
console.log(allCustomerName); उपरोक्त प्रोग्राम को चलाने के लिए, आपको निम्न कमांड का उपयोग करने की आवश्यकता है -
node fileName.js.
यहाँ मेरी फ़ाइल का नाम है demo206.js.
आउटपुट
PS C:\Users\Amit\javascript-code> node demo206.js [ 'John Smith', 'David Miller', 'Bob Taylor' ]