A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/from below:

TypedArray.from() - JavaScript | MDN

TypedArray.from()

Baseline Widely available

TypedArray.from() 정적 메서드는 순회 가능한 객체 혹은 유사 배열로부터 새로운 형식화 배열을 생성합니다. 이 메서드는 Array.from()와 거의 같습니다.

시도해 보기
const uint16 = Int16Array.from("12345");

console.log(uint16);
// Expected output: Int16Array [1, 2, 3, 4, 5]
구문 매개변수
arrayLike

형식화 배열로 변환할 순회 가능한 객체 혹은 유사 객체.

mapFn Optional

입력된 배열의 모든 요소에 대해 호출할 함수입니다. 이 함수를 제공하면 배열에 추가할 모든 값이 먼저 이 함수를 통과하고 mapFn의 반환 값이 형식화 배열에 입력 값 대신 추가됩니다. 이 함수는 다음 인수를 사용하여 호출됩니다.

element

형식화 배열에서 처리 중인 현재 요소입니다.

index

형식화 배열에서 처리 중인 현재 요소의 인덱스입니다.

thisArg Optional

mapFn을 실행할 때 this로 사용할 값입니다.

반환 값

새로운 TypedArray 인스턴스입니다.

설명

자세한 내용은 Array.from()을 참고하시기 바랍니다.

Array.from()와 TypedArray.from() 사이에는 미묘한 차이가 있습니다(참고: 아래에 언급된 this 값은 mapFn을 호출하는 데 사용된 thisArg 인수가 아니라 TypedArray.from()가 호출된 this 값입니다).

예제 순회 가능한 객체(Set)으로부터
const s = new Set([1, 2, 3]);
Uint8Array.from(s);
// Uint8Array [ 1, 2, 3 ]
문자열로부터
Int16Array.from("123");
// Int16Array [ 1, 2, 3 ]
Map과 화살표 함수와 함께 사용

요소를 조작하는 Map 함수로서 화살표 함수 사용하기

Float32Array.from([1, 2, 3], (x) => x + x);
// Float32Array [ 2, 4, 6 ]
일련의 번호 생성
Uint8Array.from({ length: 5 }, (v, k) => k);
// Uint8Array [ 0, 1, 2, 3, 4 ]
TypeArray가 아닌 생성자에서 from() 호출하기

from()의 this 값은 TypedArray 인스턴스를 반환하는 생성자여야 합니다.

function NotArray(len) {
  console.log("NotArray called with length", len);
}

Int8Array.from.call({}, []); // TypeError: #<Object> is not a constructor
Int8Array.from.call(NotArray, []);
// NotArray는 length 0으로 호출되었습니다.
// TypeError: Method %TypedArray%.from called on incompatible receiver #<NotArray>
function NotArray2(len) {
  console.log("NotArray2 called with length", len);
  return new Uint8Array(len);
}
console.log(Int8Array.from.call(NotArray2, [1, 2, 3]));
// NotArray2는 length 3으로 호출되었습니다.
// Uint8Array(3) [ 1, 2, 3 ]
명세서 브라우저 호환성 같이 보기

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4