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

জাভাস্ক্রিপ্টে JSON অবজেক্ট দুই প্রপার্টি দিয়ে গ্রুপ করে কাউন্ট করা


মান লজিয়ে, আমাদের নিচের মতো একটি অবজেক্টের অ্যারে আছে −

const arr = [
  {"location":"Kirrawee","identity_long":"student"},
  {"location":"Kirrawee","identity_long":"visitor"},
  {"location":"Kirrawee","identity_long":"visitor"},
  {"location":"Kirrawee","identity_long":"worker"},
  {"location":"Sutherland","identity_long":"student"},
  {"location":"Sutherland","identity_long":"resident"},
  {"location":"Sutherland","identity_long":"worker"},
  {"location":"Sutherland","identity_long":"resident"},
  {"location":"Miranda","identity_long":"resident"},
  {"location":"Miranda","identity_long":"worker"},
  {"location":"Miranda","identity_long":"student"},
  {"location":"Miranda","identity_long":""},
  {"location":"Miranda","identity_long":"worker"},
  {"location":"Miranda","identity_long":"resident"}
];

আমাদের একটি জাভাস্ক্রিপ্ট ফাংশন লিখতে হবে যা এমন একটি অবজেক্টের অ্যারে নেয়। ফাংশনটি একটি নতুন অবজেক্টের অ্যারে তৈরি করবে যেটিতে সব অভিন্ন (আইডেন্টিক্যাল) অবজেক্টগুলো location প্রপার্টি অনুযায়ী গ্রুপ করা থাকবে।

এবং অবজেক্টগুলোর একটি count প্রপার্টি হবে যেটিতে মূল অ্যারে-তে সেটি কতবার আসেছে তা থাকবে।

সুতরাং, উপরের অ্যারে-র জন্য আউটপুট হবে নিচের মতো −

const output = [
  {"location":"Kirrawee","identity":"student","count":1},
  {"location":"Kirrawee","identity":"visitor","count":2},
  {"location":"Kirrawee","identity":"worker","count":1},
  {"location":"Sutherland","identity":"student","count":1},
  {"location":"Sutherland","identity":"resident","count":2},
  {"location":"Sutherland","identity":"worker","count":1},
  {"location":"Miranda","identity":"resident","count":2},
  {"location":"Miranda","identity":"worker","count":2},
  {"location":"Miranda","identity":"student","count":1}
];

উদাহরণ

এটির কোড হবে −

const arr = [
  {"location":"Kirrawee","identity_long":"student"},
  {"location":"Kirrawee","identity_long":"visitor"},
  {"location":"Kirrawee","identity_long":"visitor"},
  {"location":"Kirrawee","identity_long":"worker"},
  {"location":"Sutherland","identity_long":"student"},
  {"location":"Sutherland","identity_long":"resident"},
  {"location":"Sutherland","identity_long":"worker"},
  {"location":"Sutherland","identity_long":"resident"},
  {"location":"Miranda","identity_long":"resident"},
  {"location":"Miranda","identity_long":"worker"},
  {"location":"Miranda","identity_long":"student"},
  {"location":"Miranda","identity_long":""},
  {"location":"Miranda","identity_long":"worker"},
  {"location":"Miranda","identity_long":"resident"}
];
const groupArray = (arr = []) => {
  // create map
  let map = new Map()
  for (let i = 0; i < arr.length; i++) {
    const s = JSON.stringify(arr[i]);
    if (!map.has(s)) {
      map.set(s, {
        location: arr[i].location,
        identity: arr[i].identity_long,
        count: 1,
      });
    } else {
      map.get(s).count++;
    }
  }
  const res = Array.from(map.values())
  return res;
};
console.log(groupArray(arr));

আউটপুট

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

[
  { location: 'Kirrawee', identity: 'student', count: 1 },
  { location: 'Kirrawee', identity: 'visitor', count: 2 },
  { location: 'Kirrawee', identity: 'worker', count: 1 },
  { location: 'Sutherland', identity: 'student', count: 1 },
  { location: 'Sutherland', identity: 'resident', count: 2 },
  { location: 'Sutherland', identity: 'worker', count: 1 },
  { location: 'Miranda', identity: 'resident', count: 2 },
  { location: 'Miranda', identity: 'worker', count: 2 },
  { location: 'Miranda', identity: 'student', count: 1 },
  { location: 'Miranda', identity: '', count: 1 }
]

  1. জাভাস্ক্রিপ্টে অবজেক্টে প্রোপার্টি এবং মেথড যোগ করা

  2. জাভাস্ক্রিপ্ট অবজেক্ট প্রোপার্টি

  3. জাভাস্ক্রিপ্টে অবজেক্ট তৈরি এবং প্রোপার্টি অ্যাক্সেস

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