A RetroSearch Logo

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

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/javascript-uint8array-from-method/ below:

JavaScript Uint8Array.from() Method - GeeksforGeeks

JavaScript Uint8Array.from() Method

Last Updated : 09 Jan, 2024

The Uint8Array.from() method in JavaScript is used to create a new Uint8Array from an array-like or iterable object. It allows you to transform an existing array or iterable into a new Uint8Array instance, where each element of the new array is represented as an 8-bit unsigned integer.

Syntax: 
Uint8Array.from( source, mapFn, thisArg )
Parameters:

This method accepts three parameters as mentioned above and described below:

Return Value:

This method returns a new Uint8Array instance. 

Example 1: In this example, we will see the basic functionality of the Uint8Array array to create new Uint8array from a string of integers.

javascript
// Create a Uint8Array from a 
// string like structure
let array = Uint8Array.from('45465768654323456');

// Display the result
console.log(array);

Output
Uint8Array(17) [
  4, 5, 4, 6, 5, 7, 6,
  8, 6, 5, 4, 3, 2, 3,
  4, 5, 6
]

Example 2: In this example, we will see the basic functionality of the Uint8Array array to create a new Unit8array performing the required transformations.

javascript
// Create a Uint8Array from a array by adding
// 3 to each number using function
let array = Uint8Array.from(
    [1, 2, 3, 4, 5, 6],
    (z) => z + 3
);

// Display the result
console.log(array);

Output
Uint8Array(6) [ 4, 5, 6, 7, 8, 9 ]


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