Sobes.tech
Middle+

const customMap = (array, transform) => { // array.map(transform) მსგავს; }; const stringResults = customMap([1, 2, 3], item => item.toString()); const lengths = customMap(["გამარჯობა", "მსოფლიო"], item => item.length);

sobes.tech AI

პასუხი AI-სგან

customMap ფუნქციამ უნდა მიიღოს მასივი და ტრანსფორმაციის ფუნქცია, და უნდა დააბრუნოს ახალი მასივი, სადაც თითოეული ელემენტი არის შესაბამისი ელემენტის გამოყენების შედეგი ორიგინალურ მასივზე. ეს ანალოგია სტანდარტულ Array.prototype.map-თან.

შესრულების მაგალითი:

const customMap = (array, transform) => {
  const result = [];
  for (let i = 0; i < array.length; i++) {
    result.push(transform(array[i], i, array));
  }
  return result;
};

const stringResults = customMap([1, 2, 3], item => item.toString());
console.log(stringResults); // ["1", "2", "3"]

const lengths = customMap(["hello", "world"], item => item.length);
console.log(lengths); // [5, 5]

აქ ჩვენ ხელით ვივლით მასივზე, ვაწარმოებთ ფუნქციას transform თითოეულ ელემენტზე და ვაგროვებთ შედეგებს ახალ მასივში.