Каталог
Веб-разработка с нуля: четыре курса по порядку.
HTML, затем CSS, JavaScript и SQL. Каждый курс говорит, что нужно знать до него и что вы будете уметь после.
Загружаем курсы…
Каталог
HTML, затем CSS, JavaScript и SQL. Каждый курс говорит, что нужно знать до него и что вы будете уметь после.
Загружаем курсы…
Урок 110 из 114 · 9 мин
У типизированных массивов есть почти все методы обычных: at, includes, indexOf, lastIndexOf, find, findIndex, findLast, findLastIndex, every, some, forEach, map, filter, reduce, reduceRight, join, sort, reverse, fill, copyWithin, slice, keys, values, entries, toReversed, toSorted, with, toString и toLocaleString.
Почти все методы массивов есть и у типизированных. Отличие одно: результат — снова типизированный массив того же вида.
at() · includes() · indexOf() · lastIndexOf()const t = new Uint8Array([90, 92, 95, 92]);
console.log(t.at(-1), t.includes(95), t.indexOf(92), t.lastIndexOf(92));find() · findIndex() · findLast() · findLastIndex()const t = new Uint8Array([90, 97, 95, 98]);
console.log(t.find((x) => x > 96), t.findIndex((x) => x > 96), t.findLast((x) => x > 96), t.findLastIndex((x) => x > 96));every() · some() · forEach()const t = new Uint8Array([90, 92]);
console.log(t.every((x) => x < 96), t.some((x) => x > 91));
t.forEach((x, i) => console.log(i, x));map() · filter() · reduce() · reduceRight()map и filter возвращают типизированный массив того же вида (значения обрежутся по его правилам).const t = new Uint8Array([100, 200]);
console.log(t.map((x) => x * 2), t.filter((x) => x > 150), t.reduce((a, b) => a + b, 0), t.reduceRight((a, b) => a + "," + b, ""));sort() · reverse() · fill() · copyWithin()sort() без функции сортирует ЧИСЛА (в отличие от обычных массивов).const t = new Uint8Array([10, 9, 100]);
console.log(t.sort(), t.reverse(), new Uint8Array(3).fill(7), new Uint8Array([1, 2, 3, 4]).copyWithin(0, 2));slice() · join() · toString() · toLocaleString()slice — копия (в отличие от subarray); строки — как у массивов.const t = new Float32Array([1250.5, 3]);
console.log(t.slice(1), t.join(" | "), t.toString(), t.toLocaleString("ru-RU"));keys() · values() · entries()for (const [i, v] of new Uint8Array([5, 6]).entries()) console.log(i, v);
console.log([...new Uint8Array([5, 6]).keys()], [...new Uint8Array([5, 6]).values()]);toReversed() · toSorted() · with()const t = new Uint8Array([3, 1, 2]);
console.log(t.toSorted(), t.toReversed(), t.with(0, 9), t);sort() у типизированного массива сортирует числа правильно — функция сравнения не нужна.
Для temps (Uint8Array) посчитай avg — среднюю температуру через reduce, hot — Uint8Array показаний выше 95 через filter, и sorted — отсортированную копию через toSorted.
Выберите один ответ.
Что вернёт new Uint8Array([200]).map((x) => x * 2)?
Проверяем состояние обучения…