Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Reference type-Array type

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

Each item of the ECMAScript array can hold any type of data, and the size of the array can be dynamically resized.

There are two basic ways to create an array. The first is to use the Array constructor.

1 var colors = new Array ()

Create an array of twenty items

1 var colors = new Array (20)

Create an array of three items

1 var colors = new Array ("pink", "black", "white")

Of course, the new operator can also be omitted

1 var colors = Array (3)

The second basic way to create an array is to use the literal representation of the array

1 var colors = ["red", "blue", "green"]

The number of items in the array is saved in the length property, which is not read-only, so by setting this property, you can remove items from the end of the array or add new items to the array

1 colors = 2 console.log (colors [2]); / / undefined

Detection array

For a web page or a global action, satisfactory results can be obtained by using the instanceof operator

1 if (colors instanceof Array) {/ / there is a problem when transferring in different frames! 2 alert ("This is an Array"); 3} 4

New method added in ECMAScript5! (regardless of the global execution environment in which the array was created)

1 if (Array.isArray (colors)) {/ / 2 alert ("This is an Array"); 3}

Conversion method

All objects have toLocaleString (), toString (), and valueOf () methods. Where calling the toString () method of the array returns a comma-separated string concatenated by the string of each value in the array. The call to valueOf () returns an array. In fact, the toString () method of each item is called to create this string

1 var colors = ["red", "blue", "green"]; 2 console.log (colors.toString ()); / / red,blue,green calls the toString method of each item! 3 alert (colors.valueOf ()); / / red,blue,green 4 alert (colors); / / red,blue,green

The toLocaleString () method often returns the same value as the toString () and valueOf () methods, but not always. When calling the toLocaleString () method of an array, he also creates an array-valued string that can be easily separated. Unlike before, he calls the toLocaleString () method of each item instead of the roString () method.

1 var person1 = {2 toLocaleString:function () {3 return "Nikolaos"; 4}, 5 toString:function () {6 return "Nicholas"; 7} 8}; 9 var person2 = {10 toLocaleString:function () {11 return "Grigorios" 12}, 13 toString:function () {14 return "Greg"; 15} 16}; 17 var people = [person1,person2]; 18 alert (people); / / Nicholas,Greg 19 console.log (people.toString ()); / / Nicholas,Greg 20 console.log (people.toLocaleString ()); / / Nikolaos,Grigorios

The join () method, which can build the string with a different delimiter, and if you pass no value to the join () method or undefined to it, use a comma as the delimiter

1 var colors = ["red", "blue", "green"]; 2 console.log (colors.join ("| |")); / / red | | blue | | green

Stack method

Stack is a LIFO (Last-In-First-Out) data structure, that is, the newly added items are the first to be removed.

The push () method can take any number of arguments, add them to the end one by one, and return the length of the modified array.

The pop () method removes an item from the end of the array, reduces the length value of the array, and returns the removed item

1 var colors = new Array (); 2 var count = colors.push ("red", "green"); 3 alert (count); / / 24 var item = colors.pop (); 5 alert (colors); / / "red" 6 alert (item); / / "green" 7

Queuing method

The access rule for the queue data structure is FIFO (First-In-First-Out). Add items to the column at the end of the list and remove items at the front of the list

Shift () removes the first item of the array and returns it, while the length of the array is reduced by 1.

Unshift () adds any item to the front of the array and returns the length of the array.

1 var colors = new Array (); 2 var count = colors.push ("red", "green"); 3 var item = colors.shift (); 4 alert (item); / / red5 var item = colors.unshift ("black", "pink"); 6 alert (item); / / 37 alert (colors); / / black pink green

Queues can be simulated in the opposite direction by using both unshift and pop methods

1 var colors = new Array (); 2 var count = colors.unshift ("green", "black"); 3 alert (count); / / 24 var count = colors.unshift ("pink", "white"); 5 alert (count); / / 46 var item = colors.pop (); 7 alert (item); / / black

Reordering method

Reverse () reverses the order of the array

Sort () calls the toString () method of each array item, and then sorts the array in ascending order. Sort () can take a comparison function as an argument

1 var num = [1 num.reverse (); 3 alert (num); / / 15 alert (num); / / 15 10 num.sort (); 6 alert (num); / / 1 alert (num); / / 1 alert (num)

Simple comparison function

1 function compare (result1,result2) {2 if (result1result2) {6 return 1; 7} 8 else {9 return 0 TX 10} 11} 12 var values = [0 console.log 1 5 Japanese 7 10 10 11]; 13 14 console.log (values.sort (compare)) / / [0, 1, 5, 7, 9, 10, 11]

A simpler way

1 function compare (result1,result2) {2 return result1-result2;3} 4 5 num.sort (compare); 6 alert (num); / / 1 Jing 2 Jing 3 Jing 4 Jing 10 Jing 15

Operation method

Concat () creates a new array based on all the items in the current array, specifically, this method creates a copy of the current array, then adds the received parameters to the end of the copy, and finally returns the newly built array

1 var colors = ["red", "green", "black"]; 2 var colors2 = colors.concat ("yellow", "white"); 3 alert (colors2); / / red green black yellow white

Slice () accepts one or two parameters, that is, to return the start and end positions of the item. If there is only one parameter, the slice () method returns all items from the specified position of that parameter to the end of the current array.

1 var colors = ["red", "green", "black"]; 2 var colors3 = colors.slice (1 alert 2); 3 alert (colors3); / / green black

The main purpose of splice () is to insert items into the middle of the array

When splice () accepts two parameters: delete (delete the location of the first item, the number of items deleted)

Three parameters: insert (insert position, 0, inserted item 1, inserted item 2.)

Three parameters: replace (position of replacement, 1, added item)

1 var colors = ["red", "green", "black"]; 2 var colors4 = colors.splice (1Magne1); 3 alert (colors4); / / green4 alert (colors); / / red black5 var colors4 = colors.splice (1d0, "yellow"); 6 alert (colors); / / red yellow black 7 var colors4 = colors.splice (1jue 1, "pink"); 8 alert (colors); / / red pink black

Location method

IndexOf () and lastIndexOf () take two parameters (the item to look for, where the search starts), where indexOf () looks backward from the beginning of the array, and lastIndexOf () looks forward from the end of the array. These two methods return the position of the item to be found in the array, or-1 if it is not found. The congruence operator is used when looking up

1 var numbers = [1 numbers.indexOf (4); 3 alert (item); / / 34 alert (numbers.lastIndexOf (4)); / 55 alert (numbers.indexOf (4)); / / 56 7 var person = {name: "Nicholas"}; 8 var morePerson = [person]; 9 alert (morePerson.indexOf (person)); / 0

Iterative method

ECMAScript5 defines five iterative methods for the array, each of which accepts two parameters: the function to run on each item and the scope object that runs the function-the value that affects this, and the functions passed in these methods accept three parameters: the value of the array item, the position of the item in the array, and the array object itself

Every (): runs the given function on each item in the array, and returns true if the function returns true for each item

1 var numbers = [1 var everyResult (item,index,array) {3 return (item > 2); 4}) 5 alert (everyResult); / / false

Some (): runs the given function on each item in the array, and returns true if the function returns true for any item

1 var someResult = numbers.some (function (item,index,array) {2 return (item > 2); 3}) 4 alert (someResult); / / true

Filter (): runs the given function on each item in the array and returns an array of items that will return true

1 var filterResult = numbers.filter (function (item,index,array) {2 return (item > 2); 3}) 4 alert (filterResult)

Map (): runs a given function on each item in the array and returns an array of results of each function call

1 var mapResult = numbers.map (function (item,index,array) {2 return (item*2); 3}) 4 alert (mapResult)

ForEach (): runs the given function on each item in the array

1 numbers.forEach (function (item,index,array) {2 / / perform some actions 3})

Merging method

Both the reduce () and reduceRight () methods iterate over all the items in the array and then build a final returned value. Where the reduce () method starts from the first item of the array and traverses one by one to the end, while reduceRight (), on the contrary, starts from the last item of the array and traverses forward to the first item. Both methods accept two parameters: a function called on each item and (optional) the initial value that serves as the basis for the merge. The called function takes four parameters: the previous value, the current value, the index of the item, and the array object. The first iteration occurs on the second item of the array

You can use the reduce () method to perform the operation of finding the sum of all the values in an array

1 var results = [1prev,cur,index,array 2 return prev+cur; 3 Jing 5]; 2 var sum = results.reduce (function (prev,cur,index,array) {3 return prev+cur; 4}); 5 alert (sum); / / 15 6 7 8 var sum = results.reduceRight (function (prev,cur,index,array) {9 return prec+cur;10}) 11 alert (sum); / / 15

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.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report