Skip to main content

Command Palette

Search for a command to run...

array এলেমেন্ট গুলোকে নিয়ে গ্রুপ তৈরি

Updated
2 min readView as Markdown
array এলেমেন্ট গুলোকে নিয়ে গ্রুপ তৈরি
R

FRONT-END WEB DEVELOPER

আমাদের অনেক সময় চোখে পরে কোণ কোণ ওয়েবসাইটে a দ্বারা শুরু নামের কতগুলো নাম আছে সেগুলো একজায়গায় দেখায় আবার b দ্বারা শুরু নামের কতগুলো নাম আছে সেগুলো আর এক যায়গায় দেখায় ।

A: [ 'arman', 'akash' ], B: [ 'babul', 'badol' ], C: [ 'caad', 'chinmoy' ], D: [ 'dola', 'dabul' ], E: [ 'emran', 'elon mask' ], R: [ 'rashedul', 'rahad' ], S: [ 'shihab', 'shimu' ]

এটা করার উপায় কি ? খুব সিম্পলে কিছু কোডের মাধ্যমে আমরা এটা করে ফেলতে পারি

আমাদের এখানে নামের একটা array আছে ।

const names = [
 "arman",
 "akash",
 "babul",
 "badol",
 "caad",
 "chinmoy",
 "dola",
 "dabul",
 "emran",
 "elon mask",
 "rashedul",
 "rahad",
 "shihab",
 "shimu",
];

// এখন আমাদের এভাবে সাজাতে হবে 
// A: ["arman", "akash"]
// B: ["babul", "badol"]
// C: ["caad", "chinmoy"]
// D: ["dola", "dabul"]
// E: ["emran", "elon mask"]
// R: ["rashedul", "rahad"] 
// S: ["shihab","shimu"]


const namesGroup = names.reduce((acc, curr) => {
 const firstLetter = curr[0].toUpperCase();
 if (firstLetter in acc) {
  //console.log("name is found", firstLetter);
  acc[firstLetter].push(curr); 
 } else {
  //console.log("name isn't found", firstLetter);
  acc[firstLetter] = [curr];
 }
 return acc;
}, {});

console.log(namesGroup);



// এখন এমন করে শো করাতে চাইলে কি করতে পারি ? 

// ----------- A -----------
// arman
// akash
// ----------- B -----------
// babul
// badol
// ----------- C -----------
// caad
// chinmoy
// ----------- D -----------
// dola
// dabul
// ----------- E -----------
// emran
// elon mask
// ----------- R -----------
// rashedul
// rahad
// ----------- S -----------
// shihab
// shimu


Object.keys(namesGroup).forEach((key) => {
 console.log("-----------", key, "-----------");
 namesGroup[key].forEach((name) => {
  console.log(name);
 });
});

More from this blog

Learning MySQL Database for beginners (Bangla)

today I will discuss about Database. this article can be read by all types o people, don't worry. আপনাদের প্রথমে ডাটাবেস নিয়ে পড়াশুনা করতে আসলে মনে হতে পারে এই ডাটাবেস আবার কি ? ডাটাবেস হল data + base এঁর সমন্বয়ে গঠিত একটি শব্দ । যার বাংলা অর্থ হচ্ছে...

May 24, 202317 min read85
Learning  MySQL Database for beginners  (Bangla)

simple discussion about mono repo

আমরা যখন একজন full-stack web developer তখন আমাদের backend and front-end সম্পর্কে বেশ ভালো জ্ঞান থাকতে হয় । অনেক সময় আমাদের একজকেই একটা প্রোজেক্ট এঁর backend and front-end এঁর কোড ম্যানেজ করতে হয় । বাক্তি ভেদে বিভিন্ন জন বিভিন্নভাবে প্রোজেক্টর backend...

Apr 13, 20232 min read15
simple discussion about mono repo

node js basic shorts (syntax and pattern)

এই প্রবন্ধে আমরা জানার চেষ্টা করব কে node JS এঁর কিছু কমন সিনট্যাক্স এন্ড প্যাটার্ন , যা আমরা প্রতিনিয়ত ভুলে যাই । কিভাবে নোড জেএস এ সার্ভার তৈরি করতে হয় ? const http = require("http"); const server = http.createServer((req, res) => { res.statusCode...

Apr 2, 20232 min read15
node js basic shorts (syntax and pattern)
M

MD. Rashedul Islam shihab

15 posts

Official blog of MD. Rashedul Islam Shihab, sharing practical programming tutorials, software development insights, project experiences, and the latest web technologies.