js array method
The content of this article is the js array method. Now I share it with you, and you can also give it as a reference to friends in need
Array Creation
There are two ways to create an array in JavaScript. The first is to use Array Constructor:
##1 2 3 |
var arr1 = new Array(); //Create an empty array var arr2 = new Array(20); // Create an array containing 20Array of items var arr3 = new Array("lily","lucy", "Tom"); // Create an array containing 3 strings |
The second basic way to create an array is to use array literal notation:
1 2 ##3
|
var arr4 = []; //Create an empty array var arr5 = [20]; // Create a file containing 1Array of items var arr6 = ["lily","lucy","Tom"]; // Create a containing3 array of strings |
1 ##2 3 4 |
var arr6 = ["lily" ,"lucy","Tom"]; // Create an array containing 3 strings alert(arr6[0]); //lily arr6[1] = "mary"; //Modify the second item to mary arr6[3] = "sean"; //Add the fourth item to sean |
The length attribute of the array in can be modified, see the following example:
2 ##3
| var
arr = ["lily","lucy", "Tom"]; // Create an array containing 3 stringsarr[arr.length] = "sean"; // in subscript Add an item "sean"arr.length = arr to 3 (that is, the end of the array) .length-1; //Delete the last item of the array If you need to determine whether an object is an array object, before ECMAScript 5, we can use instanceof Array To judge, but the problem with the instanceof operator is that it assumes that there is only one global execution environment. If the web page contains multiple frames, there are actually more than two different global execution environments, and thus more than two different versions of the Array constructor. If you pass an array from one frame to another, the array you pass in will have a different constructor than the array created natively in the second frame. ECMAScript 5 Newly added Array.isArray() method. The purpose of this method is to ultimately determine whether a value is an array, regardless of the global execution context in which it was created. Array method #The following is an introduction to the array method. The array method includes the array prototype method, and also from object Methods inherited from objects. Here we only introduce the prototype methods of arrays. The main array prototype methods are as follows: join() New method browser support for ES5: Opera 11+ For supported browser versions, you can pass ArrayPrototype extension to achieve. The basic functions of each method are introduced in detail below. 1、join() join(separator ): Combine the elements of the array into a string, using separator as the separator. If omitted, the default comma is used as the separator. This method only Receives one parameter: the delimiter.
join() method, just pass in the string and the repeated times, the repeated string can be returned. The function is as follows:
|
The above is the detailed content of js array method. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Unfortunately, people often delete certain contacts accidentally for some reasons. WeChat is a widely used social software. To help users solve this problem, this article will introduce how to retrieve deleted contacts in a simple way. 1. Understand the WeChat contact deletion mechanism. This provides us with the possibility to retrieve deleted contacts. The contact deletion mechanism in WeChat removes them from the address book, but does not delete them completely. 2. Use WeChat’s built-in “Contact Book Recovery” function. WeChat provides “Contact Book Recovery” to save time and energy. Users can quickly retrieve previously deleted contacts through this function. 3. Enter the WeChat settings page and click the lower right corner, open the WeChat application "Me" and click the settings icon in the upper right corner to enter the settings page.

Mobile games have become an integral part of people's lives with the development of technology. It has attracted the attention of many players with its cute dragon egg image and interesting hatching process, and one of the games that has attracted much attention is the mobile version of Dragon Egg. To help players better cultivate and grow their own dragons in the game, this article will introduce to you how to hatch dragon eggs in the mobile version. 1. Choose the appropriate type of dragon egg. Players need to carefully choose the type of dragon egg that they like and suit themselves, based on the different types of dragon egg attributes and abilities provided in the game. 2. Upgrade the level of the incubation machine. Players need to improve the level of the incubation machine by completing tasks and collecting props. The level of the incubation machine determines the hatching speed and hatching success rate. 3. Collect the resources required for hatching. Players need to be in the game

Setting font size has become an important personalization requirement as mobile phones become an important tool in people's daily lives. In order to meet the needs of different users, this article will introduce how to improve the mobile phone use experience and adjust the font size of the mobile phone through simple operations. Why do you need to adjust the font size of your mobile phone - Adjusting the font size can make the text clearer and easier to read - Suitable for the reading needs of users of different ages - Convenient for users with poor vision to use the font size setting function of the mobile phone system - How to enter the system settings interface - In Find and enter the "Display" option in the settings interface - find the "Font Size" option and adjust it. Adjust the font size with a third-party application - download and install an application that supports font size adjustment - open the application and enter the relevant settings interface - according to the individual

The method of using a foreach loop to remove duplicate elements from a PHP array is as follows: traverse the array, and if the element already exists and the current position is not the first occurrence, delete it. For example, if there are duplicate records in the database query results, you can use this method to remove them and obtain results without duplicate records.

Methods for deep copying arrays in PHP include: JSON encoding and decoding using json_decode and json_encode. Use array_map and clone to make deep copies of keys and values. Use serialize and unserialize for serialization and deserialization.

The performance comparison of PHP array key value flipping methods shows that the array_flip() function performs better than the for loop in large arrays (more than 1 million elements) and takes less time. The for loop method of manually flipping key values takes a relatively long time.

Multidimensional array sorting can be divided into single column sorting and nested sorting. Single column sorting can use the array_multisort() function to sort by columns; nested sorting requires a recursive function to traverse the array and sort it. Practical cases include sorting by product name and compound sorting by sales volume and price.

PHP's array_group_by function can group elements in an array based on keys or closure functions, returning an associative array where the key is the group name and the value is an array of elements belonging to the group.
