Get the App
SLTechnology News&Howtos  ›  Development  › 

How JavaScript merges arrays with cropped arrays

Shulou Source: shulou.com Published: 2022-06-01 04:16:24 09月15日 Update

In this article Xiaobian for you to introduce in detail "JavaScript how to merge arrays and cut arrays", the content is detailed, the steps are clear, the details are handled properly, I hope this "JavaScript how to merge arrays and cut arrays" article can help you solve doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.

Merge (join) array

The concat () method creates a new array by merging (concatenating) existing arrays:

Instance (merging two arrays)

Var myGirls = ["Cecilie", "Lone"]

Var myBoys = ["Emil", "Tobias", "Linus"]

Var myChildren = myGirls.concat (myBoys); / / Connect myGirls and myBoys

The concat () method does not change the existing array. It always returns a new array.

The concat () method can use any number of array parameters:

Instance (merging three arrays)

Var arr1 = ["Cecilie", "Lone"]

Var arr2 = ["Emil", "Tobias", "Linus"]

Var arr3 = ["Robin", "Morgan"]

Var myChildren = arr1.concat (arr2, arr3); / / Connect arr1, arr2 and arr3 together

The concat () method can also take a value as a parameter:

Instance (merging arrays with values)

Var arr1 = ["Cecilie", "Lone"]

Var myChildren = arr1.concat (["Emil", "Tobias", "Linus"])

Crop array

The slice () method cuts out a new array with a fragment of the array.

This example cuts out an array starting with array element 1 ("Orange"):

Example

Var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]

Var citrus = fruits.slice (1)

The slice () method creates a new array. It does not remove any elements from the source array.

This example cuts out an array starting with array element 3 ("Apple"):

Example

Var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]

Var citrus = fruits.slice (3)

Slice () accepts two parameters, such as (1, 3).

This method selects the element from the start parameter until the end parameter (not included).

Example

Var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]

Var citrus = fruits.slice (1,3)

If the end parameter is omitted, such as the first example, slice () cuts out the rest of the array.

Example

Var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]

Var citrus = fruits.slice (2)

Automatic toString ()

If the original value is required, JavaScript automatically converts the array to a string. The following two examples will produce the same results:

Example

Var fruits = ["Banana", "Orange", "Apple", "Mango"]

Document.getElementById ("demo"). InnerHTML = fruits.toString ()

Example

Var fruits = ["Banana", "Orange", "Apple", "Mango"]

Document.getElementById ("demo"). InnerHTML = fruits

All JavaScript objects have the toString () method.

After reading this, the article "how to merge arrays and cut arrays in JavaScript" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, please follow the industry information channel.

Tags: Array instance method parameter element two article example content original proper same three character string object idea quantity new knowledge more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Tech Info MySQL Huawei vpn