সমস্যা
আমাদের একটি জাভাস্ক্রিপ্ট ফাংশন লিখতে হবে যা মিশ্র ডেটা টাইপের একটি অ্যারে নেবে। ফাংশনটি একটি অবজেক্ট ফেরত দেবে যেখানে কী হিসেবে ডেটা টাইপের নাম থাকবে এবং মান হিসেবে সেই টাইপের সকল উপাদানের অ্যারে থাকবে।
উদাহরণ
নিচে কোডটি দেওয়া হলো:
const arr = [1, 'a', [], '4', 5, 34, true, undefined, null];
const groupDataTypes = (arr = []) => {
const res = {};
for(let i = 0; i < arr.length; i++){
const el = arr[i];
const type = typeof el;
if(res.hasOwnProperty(type)){
res[type].push(el);
}else{
res[type] = [el];
}
}
return res;
};
console.log(groupDataTypes(arr));আউটপুট
কনসোল আউটপুট:
{
number: [ 1, 5, 34 ],
string: [ 'a', '4' ],
object: [ [], null ],
boolean: [ true ],
undefined: [ undefined ]
}