ধরা যাক, у нас দুটি অ্যারে আছে এরকম -
const meals = ["breakfast", "lunch", "dinner"]; const ingredients = [ ["eggs", "yogurt", "toast"], ["falafel", "mushrooms", "fries"], ["pasta", "cheese"] ];
আমাদের একটি জাভাস্ক্রিপ্ট ফাংশন লিখতে হবে যা এমন দুটি অ্যারে নেবে এবং দ্বিতীয় অ্যারেতeki সাব-অ্যারেগুলোকে প্রথম অ্যারেতeki সংশ্লিষ্ট স্ট্রিংগুলোর সাথে ম্যাপ করবে।
সুতরাং, উপরের অ্যারেগুলোর জন্য আউটপুট দেখতে হবে এরকম -
const output = {
"breakfast" : ["eggs", "yogurt", "toast"],
"lunch": ["falafel", "mushrooms", "fries"],
"dinner": ["pasta", "cheese"]
};
উদাহরণ
কোডটি হবে -
const meals = ["breakfast", "lunch", "dinner"];
const ingredients = [
["eggs", "yogurt", "toast"],
["falafel", "mushrooms", "fries"],
["pasta", "cheese"]
];
const combineMealAndIngredient = (meals, ingredients) => {
const res = {};
meals.forEach(function (el, ind) {
this[el] = ingredients[ind];
}, res);
return res;
};
console.log(combineMealAndIngredient(meals, ingredients));
আউটপুট
কনসোলে আউটপুট হবে -
{
breakfast: [ 'eggs', 'yogurt', 'toast' ],
lunch: [ 'falafel', 'mushrooms', 'fries' ],
dinner: [ 'pasta', 'cheese' ]
}