কম্পিউটার টিউটোরিয়াল

সর্বোচ্চ কী মান সহ অবজেক্ট অ্যারে থেকে রিটার্ন করা - জাভাস্ক্রিপ্ট

ধরা যাক, আমাদের কাছে অবজেক্টের একটি অ্যারে রয়েছে যাতে কিছু ছাত্রের পরীক্ষার মার্কসের তথ্য আছে:

const students = [
  { name: 'Andy', total: 40 },
  { name: 'Seric', total: 50 },
  { name: 'Stephen', total: 85 },
  { name: 'David', total: 30 },
  { name: 'Phil', total: 40 },
  { name: 'Eric', total: 82 },
  { name: 'Cameron', total: 30 },
  { name: 'Geoff', total: 30 }
];

আমাদের একটি জাভাস্ক্রিপ্ট ফাংশন লিখতে হবে যা এমন একটি অ্যারে নেয় এবং সবচেয়ে বেশি total মানযুক্ত ছাত্রের নাম এবং টোটাল ধারণকারী একটি অবজেক্ট রিটার্ন করবে।

তাহলে, উপরের অ্যারেটির জন্য আউটপুট হবে:

{ name: 'Stephen', total: 85 }

উদাহরণ

নিচের কোডটি দেখুন:

const students = [
  { name: 'Andy', total: 40 },
  { name: 'Seric', total: 50 },
  { name: 'Stephen', total: 85 },
  { name: 'David', total: 30 },
  { name: 'Phil', total: 40 },
  { name: 'Eric', total: 82 },
  { name: 'Cameron', total: 30 },
  { name: 'Geoff', total: 30 }
];

const pickHighest = arr => {
  const res = {
    name: '',
    total: -Infinity
  };
  arr.forEach(el => {
    const { name, total } = el;
    if(total > res.total){
      res.name = name;
      res.total = total;
    };
  });
  return res;
};

console.log(pickHighest(students));

আউটপুট

এটি কনসোলে নিচের আউটপুট তৈরি করবে:

{ name: 'Stephen', total: 85 }
  1. জাভাস্ক্রিপ্ট দিয়ে এলিমেন্ট থেকে ক্লাস নাম মুছে ফেলা

  2. জাভাস্ক্রিপ্ট অবজেক্ট অ্যারে কিনা যাচাই করার পদ্ধতি

  3. জাভাস্ক্রিপ্টে অবজেক্ট 머জ করে সিঙ্গেল অবজেক্ট অ্যারে তৈরি করা

  4. জাভাস্ক্রিপ্টে নাল/খালি অবজেক্টযুক্ত অ্যারেতে ভ্যালু অ্যাসাইন করা