ধরুন, আমাদের কাছে একটি অ্যারে-র অ্যারে আছে −
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 } ]