In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "the use of JS array Array". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Convert an array to a string
The JavaScript toString () method converts an array to a string of (comma-separated) array values.
Example:
Var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById ("demo") [xss_clean] = fruits.toString ()
The join () method also concatenates all array elements into a string. It behaves like toString (), but you can also specify the delimiter:
Var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById ("demo") [xss_clean] = fruits.join ("*")
Pop () and push ()
When using arrays, it is easy to delete elements and add new elements. This is the pop () method and push () method.
The pop () method removes the last element from the array:
Var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.pop (); / / remove the last element from fruits ("Mango")
The pop () method returns the value of "pop":
Var fruits = ["Banana", "Orange", "Apple", "Mango"]; var x = fruits.pop (); the value of / / x is "Mango"
The push () method adds a new element to the end of the array:
Var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.push ("Kiwi"); / / add a new element ("Kiwi") to the end of the fruits
The push () method returns the new array length:
Var fruits = ["Banana", "Orange", "Apple", "Mango"]; var x = fruits.push ("Kiwi"); the value of / / x is 5
Shift () and unshift ()
The shift () method deletes the first array element and "shifts" all other elements to the lower index.
Var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.shift (); / / Delete the first element "Banana"
The shift () method returns the string of "move out":
Var fruits = ["Banana", "Orange", "Apple", "Mango"]; var x = fruits.shift (); the value of / / x is "Banana"
The unshift () method adds a new element to the beginning of the array and moves the old element to a higher index:
Var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.unshift ("Lemon"); / / add a new element "Lemon" to fruits
The unshift () method returns the new array length.
Var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.unshift ("Lemon"); / / returns 5
Change element
Use index numbers to access array elements:
The array index starts with 0. [0] is the first array element, [1] is the second, and [2] is the third.
Var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits [0] = "Kiwi"; / / change the first element of fruits to "Kiwi"
The length attribute provides an easy way to attach new elements to an array:
Var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits [fruits.length] = "Kiwi"; / / append "Kiwi" to fruits
Delete element
Because the JavaScript array is an object, you can delete an element using the JavaScript operator delete:
Var fruits = ["Banana", "Orange", "Apple", "Mango"]; delete fruits [0]; / / change the first element in fruits to undefined
Using delete may leave holes for undefined in the array. Use pop () or shift () instead.
Splicing array
The splice () method can be used to add new items to an array:
Var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.splice (2,0, "Lemon", "Kiwi")
The first parameter (2) defines the position where the new component should be added (in the engagement). The second parameter (0) defines how many elements should be deleted. The remaining parameters ("Lemon", "Kiwi") define the new elements to be added. The splice () method returns an array of deleted items:
Var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.splice (2,2, "Lemon", "Kiwi")
Delete an element using splice ()
With clever parameter settings, you can use splice () to delete elements without leaving "holes" in the array:
Var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.splice (0,1); / / Delete the first element of fruits
The first parameter (0) defines where the new element should be added. The second parameter (1) defines how many elements should be deleted. The remaining parameters are omitted. No new elements are added.
Merge array
Create a new array by merging the existing array with the concat () method:
Var myGirls = ["Cecilie", "Lone"]; var myBoys = ["Emil", "Tobias", "Linus"]; var myChildren = myGirls.concat (myBoys); / / merge myGirls and myBoys arrays
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
Var arr1 = ["Cecilie", "Lone"]; var arr2 = ["Emil", "Tobias", "Linus"]; var arr3 = ["Robin", "Morgan"]; var myChildren = arr1.concat (arr2, arr3); / / merge arr1 and arr2 and arr3
The concat () method can also take a value as a parameter:
Var arr1 = ["Cecilie", "Lone"]; var myChildren = arr1.concat (["Emil", "Tobias", "Linus"])
Array slice
The slice () method cuts an array into a new array. This example cuts out part of the array starting with array element 1 ("Orange"):
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 part of the array starting with array element 3 ("Apple"):
Var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]; var citrus = fruits.slice (3)
The slice () method can take two parameters, slice (start,end). Method selects an element from the start parameter up to, but not including, the end parameter.
Var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]; var citrus = fruits.slice (1,3)
If you omit the end parameter, as in the first example, the slice () method cuts off the rest of the array.
Var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]; var citrus = fruits.slice (2)
Automatic toString ()
When the original value is required, JavaScript automatically converts the array to a comma-separated string. This is always the case when trying to output an array. These two examples will produce the same result:
Var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById ("demo") [xss_clean] = fruits.toString (); var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById ("demo") [xss_clean] = fruits; "usage of JS array Array". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.