সম্পত্তি যোগ করতে, মানচিত্র() ব্যবহার করুন। ধরা যাক নিম্নলিখিতটি আমাদের অ্যারে -
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' }
]