प्रॉपर्टी जोड़ने के लिए map() का उपयोग करें। मान लीजिए निम्नलिखित हमारी सरणी है:
const firstname = ['John', 'David', 'Bob'];
निम्नलिखित हमारे ऑब्जेक्ट्स की सरणी हैं:
const studentDetails = [
{ firstname: 'Carol', marks: 78 },
{ firstname: 'Mike', marks: 89 },
{ firstname: 'Bob', marks: 86 }
];
उदाहरण
निम्नलिखित कोड देखें:
const firstname = ['John', 'David', 'Bob'];
const studentDetails = [
{ firstname: 'Carol', marks: 78 },
{ firstname: 'Mike', marks: 89 },
{ firstname: 'Bob', marks: 86 }
];
const data = new Set(firstname);
const result = studentDetails.map(tmpObject => {
if (data.has(tmpObject.firstname))
tmpObject.isPresent = "This is present";
else
tmpObject.isPresent = "This is not present";
return tmpObject;
});
console.log(result);
उपरोक्त प्रोग्राम चलाने के लिए, आपको निम्नलिखित कमांड का उपयोग करना होगा:
node fileName.js.
यहां, मेरा फ़ाइल नाम demo219.js है।
आउटपुट
आउटपुट इस प्रकार है:
PS C:\Users\Amit\JavaScript-code> node demo219.js
[
{ firstname: 'Carol', marks: 78, isPresent: 'This is not present' },
{ firstname: 'Mike', marks: 89, isPresent: 'This is not present' },
{ firstname: 'Bob', marks: 86, isPresent: 'This is present' }
]