কম্পিউটার টিউটোরিয়াল

জাভাস্ক্রিপ্ট: দুটি অ্যারে লিংক করে JSON অবজেক্টের অ্যারে তৈরি করুন


ধরা যাক, у нас দুটি অ্যারে আছে এরকম -

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' ]
}

  1. জাভাস্ক্রিপ্টে দুটি অ্যারে মার্জ করার পদ্ধতি

  2. জাভাস্ক্রিপ্টে টু-ডাইমেনশনাল অ্যারে তৈরি করা

  3. জাভাস্ক্রিপ্ট JSON অ্যারে

  4. জাভাস্ক্রিপ্ট ব্যবহার করে JSON অ্যারে থেকে ডেটা পড়া