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

জাভাস্ক্রিপ্টে অবজেক্টের অ্যারেতে সর্বোচ্চ ভ্যালু ধারণকারী অবজেক্ট খুঁজে বের করা

আমাদের একটি অ্যারে আছে যেটিতে বেশ কিছু student অবজেক্ট আছে, প্রতিটি student অবজেক্টে grades নামে একটি অ্যারে প্রপার্টি রয়েছে:

const arr = [
  {
    name: "Student 1",
    grades: [ 65, 61, 67, 70 ]
  },
  {
    name: "Student 2",
    grades: [ 50, 51, 53, 90 ]
  },
  {
    name: "Student 3",
    grades: [ 0, 20, 40, 60 ]
  }
];

আমাদের একটি ফাংশন তৈরি করতে হবে যা student অ্যারেতে লুপ করে যেকোনো student অবজেক্টের grades অ্যারেতে সর্বোচ্চ গ্রেড আছে সেটি খুঁজে বের করবে।

উদাহরণ

কোডটি হবে:

const arr = [
  {
    name: "Student 1",
    grades: [ 65, 61, 67, 70 ]
  },
  {
    name: "Student 2",
    grades: [ 50, 51, 53, 90 ]
  },
  {
    name: "Student 3",
    grades: [ 0, 20, 40, 60 ]
  }
];
const highestGrades = arr.map((stud, ind) => {
  return {
    name: stud.name,
    highestGrade: Math.max.apply(Math, stud.grades) // get a student's
    highest grade
  };
});
const bestStudent = highestGrades.sort((a, b) => {
  return b.highestGrade - a.highestGrade;
})[0];
console.log(bestStudent.name + " has the highest score of " + bestStudent.highestGrade);

আউটপুট

কনসোলে আউটপুট হবে:

Student 2 has the highest score of 90
  1. জাভাস্ক্রিপ্টে map বা forEach ব্যবহার করে অবজেক্টের ভেতরের অ্যারে অফ অবজেক্ট পার্স করা

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

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

  4. জাভাস্ক্রিপ্টে অ্যারে থেকে সর্বোচ্চ মান বের করা