ধরুন আমাদের কাছে এই −
এর মতো অ্যারের অ্যারে আছেconst arr = [ [ ['juice', 'apple'], ['maker', 'motts'], ['price', 12] ], [ ['juice', 'orange'], ['maker', 'sunkist'], ['price', 11] ] ];
আমাদের একটি জাভাস্ক্রিপ্ট ফাংশন লিখতে হবে যা এই ধরনের একটি অ্যারে নেয় এবং ইনপুট অ্যারের উপর ভিত্তি করে নির্মিত বস্তুর একটি নতুন অ্যারে প্রদান করে৷
সুতরাং, উপরের অ্যারের জন্য, আউটপুটটি এইরকম হওয়া উচিত −
const output = [
{juice: 'apple', maker: 'motts', price: 12},
{juice: 'orange', maker: 'sunkist', price: 11}
]; উদাহরণ
এর জন্য কোড হবে −
const arr = [
[
['juice', 'apple'], ['maker', 'motts'], ['price', 12]
],
[
['juice', 'orange'], ['maker', 'sunkist'], ['price', 11]
]
];
const arrayToObject = arr => {
let res = [];
res = arr.map(list => {
return list.reduce((acc, val) => {
acc[val[0]] = val[1];
return acc;
}, {});
});
return res;
};
console.log(arrayToObject(arr)); আউটপুট
কনসোলে আউটপুট -
[
{ juice: 'apple', maker: 'motts', price: 12 },
{ juice: 'orange', maker: 'sunkist', price: 11 }
][
{ juice: 'apple', maker: 'motts', price: 12 },
{ juice: 'orange', maker: 'sunkist', price: 11 }
]