| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/at | [Back] [Original] |
Get to know MDN better
Dieser Inhalt wurde automatisch aus dem Englischen bersetzt, und kann Fehler enthalten. Erfahre mehr ber dieses Experiment.
Diese Funktion ist gut etabliert und funktioniert auf vielen Gerten und in vielen Browserversionen. Sie ist seit Mrz 2022 browserbergreifend verfgbar.
Die at() Methode von TypedArray Instanzen nimmt einen ganzzahligen Wert und gibt das Element an diesem Index zurck, wobei positive und negative Ganzzahlen zulssig sind. Negative Ganzzahlen zhlen vom letzten Element im typisierten Array rckwrts. Diese Methode verwendet denselben Algorithmus wie Array.prototype.at().
const int8 = new Int8Array([0, 10, -10, 20, -30, 40, -50]);
let index = 1;
console.log(`An index of ${index} returns the item ${int8.at(index)}`);
// Expected output: "An index of 1 returns the item 10"
index = -2;
console.log(`An index of ${index} returns the item ${int8.at(index)}`);
// Expected output: "An index of -2 returns the item 40"
at(index)
indexNullbasierter Index des typisierten Array-Elements, das zurckgegeben werden soll, in eine Ganzzahl umgewandelt. Ein negativer Index zhlt vom Ende des typisierten Arrays zurck wenn index < 0, wird index + array.length aufgerufen.
Das Element im typisierten Array, das dem gegebenen Index entspricht. Gibt immer undefined zurck, wenn index < -array.length oder index >= array.length, ohne zu versuchen, auf die entsprechende Eigenschaft zuzugreifen.
Siehe Array.prototype.at() fr weitere Details. Diese Methode ist nicht generisch und kann nur auf Instanzen von typisierten Arrays aufgerufen werden.
Das folgende Beispiel bietet eine Funktion, die das letzte in einem angegebenen Array gefundene Element zurckgibt.
const uint8 = new Uint8Array([1, 2, 4, 7, 11, 18]);
// A function which returns the last item of a given array
function returnLast(arr) {
return arr.at(-1);
}
const lastItem = returnLast(uint8);
console.log(lastItem); // 18
Hier vergleichen wir verschiedene Mglichkeiten, das vorletzte (das vor dem letzten) Element eines TypedArray auszuwhlen. Whrend alle unten aufgefhrten Methoden gltig sind, wird die Prgnanz und Lesbarkeit der at() Methode hervorgehoben.
// Our typed array with values
const uint8 = new Uint8Array([1, 2, 4, 7, 11, 18]);
// Using length property
const lengthWay = uint8[uint8.length - 2];
console.log(lengthWay); // 11
// Using slice() method. Note an array is returned
const sliceWay = uint8.slice(-2, -1);
console.log(sliceWay[0]); // 11
// Using at() method
const atWay = uint8.at(-2);
console.log(atWay); // 11
| Spezifikation |
|---|
| ECMAScript 2027 LanguageSpecification # sec-%typedarray%.prototype.at |
TypedArray.prototype.at in core-jsTypedArray.prototype.atTypedArrayTypedArray.prototype.findIndex()TypedArray.prototype.indexOf()TypedArray.prototype.with()Array.prototype.at()String.prototype.at()TypedArrayObject/FunctionBigInt64ArrayBigUint64ArrayFloat16ArrayFloat32ArrayFloat64ArrayInt8ArrayInt16ArrayInt32ArrayUint8ArrayUint8ClampedArrayUint16ArrayUint32ArrayDer Bauplan fr ein besseres Internet.
Teile dieses Inhalts sind 19982026 von einzelnen mozilla.org-Mitwirkenden. Inhalte sind verfgbar unter einer Creative-Commons-Lizenz.
| Web Proxy Viewer | New URL | Original Page |