मान लीजिए निम्नलिखित हमारा JSON ऐरे है -
var details = [
{ "customerDetails": [ { "customerName": "John Smith", "customerCountryName": "US" } ] },
{ "customerDetails": [ { "customerName": "David Miller", "customerCountryName": "AUS" } ] },
{ "customerDetails": [ { "customerName": "Bob Taylor", "customerCountryName": "UK" } ] }
]केवल CustomerName वैल्यूज़ प्राप्त करने के लिए, map() की अवधारणा का उपयोग करें -
उदाहरण
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' ]