Middle+
const customMap = (array, transform) => { // array.map(transform) kimi bir şey; }; const stringResults = customMap([1, 2, 3], item => item.toString()); const lengths = customMap(["salam", "dünya"], item => item.length);
sobes.tech Süni İntellekt
AI-dan cavab
customMap funksiyası massiv və çevirmə funksiyasını qəbul etməli və hər elementə tətbiq olunan funksiyanın nəticəsi olan yeni bir massiv qaytarmalıdır. Bu, standart Array.prototype.map ilə eynidir.
Tətbiq nümunəsi:
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]
Burada biz massiv üzərində əl ilə iterasiya edirik, transform funksiyasını hər elementə tətbiq edirik və nəticələri yeni bir massivdə toplayırıq.