Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add new toSorted, toSpliced and toReversed methods to "Array methods" article#3622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
Hasan-Mir wants to merge1 commit intojavascript-tutorial:master
base:master
Choose a base branch
Loading
fromHasan-Mir:patch-1
Open
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions1-js/05-data-types/05-array-methods/article.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -96,6 +96,18 @@ arr.splice(2, 0, "complex", "language");

alert( arr ); // "I", "study", "complex", "language", "JavaScript"
```
````smart header="toSpliced"
A new method `toSpliced` is available that works like `splice` but does not mutate the original array(it is the [copying](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#copying_methods_and_mutating_methods) version of the `splice`). Instead, it returns a new array with the desired changes.

```js run
let arr = ["I", "study", "JavaScript"];

const splicedArr = arr.toSpliced(1, 1); // from index 1 remove 1 element

alert(arr); // ["I", "study", "JavaScript"]
alert(splicedArr); // ["I", "JavaScript"]
```
````

````smart header="Negative indexes allowed"
Here and in other array methods, negative indexes are allowed. They specify the position from the end of the array, like here:
Expand DownExpand Up@@ -505,6 +517,20 @@ alert( countries.sort( (a, b) => a.localeCompare(b) ) ); // Andorra,Österreich,
```
````

````smart header="toSorted"
A new method `toSorted` is available that works like `sort` but does not mutate the original array(it is the [copying](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#copying_methods_and_mutating_methods) version of the `sort`). Instead, it returns a new array with the sorted elements.

```js run
let numbers = [3, 2, 1];

const sortedNumbers = numbers.toSorted();

alert(numbers); // [3, 2, 1]
alert(sortedNumbers); // [1, 2, 3]
```
````


### reverse

The method [arr.reverse](mdn:js/Array/reverse) reverses the order of elements in `arr`.
Expand All@@ -520,6 +546,19 @@ alert( arr ); // 5,4,3,2,1

It also returns the array `arr` after the reversal.

````smart header="toReversed"
A new method `toReversed` is available that works like `reverse` but does not mutate the original array(it is the [copying](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#copying_methods_and_mutating_methods) version of the `reverse`). Instead, it returns a new array with the reversed elements.

```js run
let arr = [3, 2, 1];

const reversedArr = arr.toReversed();

alert(arr); // [3, 2, 1]
alert(reversedArr); // [1, 2, 3]
```
````

### split and join

Here's the situation from real life. We are writing a messaging app, and the person enters the comma-delimited list of receivers: `John, Pete, Mary`. But for us an array of names would be much more comfortable than a single string. How to get it?
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp