This article mainly shares with you the operation methods of arrays and strings in js, mainly in the form of text and code. I hope it can help everyone.
1. Array operation method
// 1.數(shù)組的操作方法 var a = []; a.unshift() /*在數(shù)組的開頭添加一個或者多個元素,返回新長度;IE9+*/ a.shift() /*刪除數(shù)組中的第一個元素,返回刪除的元素*/ a.push() /*往數(shù)組的末尾添加一個或多個元素,返回新長度*/ a.pop() /*刪除并返回數(shù)組的最后一個元素*/ a.slice('start', 'end') /*不修改原數(shù)組,返回一個新數(shù)組*/ a.concat() /*不修改原數(shù)組,返回一個新數(shù)組。不傳參數(shù)時,復(fù)制數(shù)組;傳參數(shù)時連接數(shù)組,可以有多個數(shù)組參數(shù);*/ a.splice() /*刪除數(shù)組:兩個參數(shù),第一個是開始的位置,第二個是刪除的長度 插入元素:至少3個參數(shù),第一個是開始的位置,第二個是0,后面是要插入的元素(可以有多個) 替換元素:至少3個參數(shù),第一個是開的位置,第二個是替換的個數(shù),后面是要替換的元素*/ a.reverse() /*反轉(zhuǎn)數(shù)組*/ a.sort() /*用特定的方法對數(shù)組進(jìn)行排列,接收一個排序函數(shù),該函數(shù)接收兩個參數(shù), 如果第一個參數(shù)在前面,返回一個負(fù)數(shù);如果第二個參數(shù)在前面,返回一個正數(shù);如果兩個參數(shù)相等,返回0*/ a.indexOf() /*在數(shù)組中查找元素,兩個參數(shù),第一個參數(shù)時要查找的元素;第二個參數(shù)是要開始查找的位置(該參數(shù)可以沒有)*/ a.lastIndexOf() /*從數(shù)組的后面開始查找元素,與indexOf相同,只是查找方向不同*/ a.join() /*把數(shù)組格式化成字符串,接收一個參數(shù),即:分隔符。如果不傳參數(shù)或傳入undefined,則以逗號分隔*/ a.toString() /*返回以逗號為分隔符的數(shù)組中的元素組成的字符串*/ a instanceof Array /*檢測是否為數(shù)組,如果是,返回true*/ Array.isArray(a) /*檢測是否為數(shù)組,如果是,返回true。IE9+*/
2. String operation method
// 字符串操作方法 var b = '54545'; b.concat() /*拼接字符串,返回新的字符串,不改變原字符串。實踐中最常用的是+操作符*/ b.charAt() /*接收一個參數(shù)(字符串中的位置),返回字符串中某位置上的字符*/ b.charCodeAt() /*與charAt用法相同,返回的是該字符的字符編碼*/ String.fromCharCode() /*傳入字符串的編碼,把編碼解析為字符串并返回該字符串??山邮斩鄠€編碼參數(shù)*/ b.slice('start', 'end') /*返回一個新的字符串,不不包含end,不改變原字符串*/ b.substring('start', 'end') /*與slice一樣*/ b.substr('start', 'length') /*返回一個新字符串,不改變原字符串*/ b.indexOf() /*在字符串中從前往后查找字符,沒找到就返回-1。接收兩個參數(shù):第一個參數(shù)時要查找的字符;第二個參數(shù)是(可選的)開始查找的位置*/ b.lastIndexOf() /*與indexOf用法相同,改方法是從后往前查找*/ b.search() /*在字符串中查找,并返回位置。接收一個參數(shù)(要查找的字符串),如果沒找到,返回-1.從左向右查找*/ b.trim() /*創(chuàng)建一個字符串副本,刪除前置及后綴的所有空格,返回結(jié)果。 IE9+*/ b.toLocaleLowerCase() /*字符串大寫轉(zhuǎn)為小寫(針對地區(qū)特定的方法,推薦使用)*/ b.toLocaleUpperCase() /*字符串小寫轉(zhuǎn)為大寫(針對地區(qū)特定的方法,推薦使用)*/ b.toLowerCase() /*字符串大寫轉(zhuǎn)為小寫*/ b.toUpperCase() /*字符串小寫轉(zhuǎn)為大寫*/ b.localeCompare('str') /*比較字符串b與str,并返回0或一個正數(shù)或一個負(fù)數(shù)。如果在字母表中b在str的前面,就返回一個負(fù)數(shù);在后面,返回一個正數(shù);相等返回0*/ b.replace() /*查找并替換(不改變原字符串),返回一個新字符串,接收兩個參數(shù)。第一個參數(shù):要替換(查找)的字符串;第二個:要替換成的字符串。*/ // 例子: function replac() { var text = "cat,bat,sat,fat"; var result1 = text.replace('at', 'ond'); /*第一個參數(shù)是字符串*/ console.log(result1); /*"cond,bat,sat,fat"*/ /*只替換第一個*/ var result2 = text.replace(/at/g, 'ond'); /*第一個參數(shù)是正則表達(dá)式*/ console.log(result2); /*"cond,bond,sond,fond"*/ /*全部替換*/ console.log(text); }; replac();
Related recommendations:
JS interception and splitting characters Common methods for strings
JS string to remove duplicate characters
js implements string conversion to date format
The above is the detailed content of How to operate arrays and strings in js. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

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.

Tomato Novel is a very popular novel reading software. We often have new novels and comics to read in Tomato Novel. Every novel and comic is very interesting. Many friends also want to write novels. Earn pocket money and edit the content of the novel you want to write into text. So how do we write the novel in it? My friends don’t know, so let’s go to this site together. Let’s take some time to look at an introduction to how to write a novel. Share the Tomato novel tutorial on how to write a novel. 1. First open the Tomato free novel app on your mobile phone and click on Personal Center - Writer Center. 2. Jump to the Tomato Writer Assistant page - click on Create a new book at the end of the novel.

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

1. First open pycharm and enter the pycharm homepage. 2. Then create a new python script, right-click - click new - click pythonfile. 3. Enter a string, code: s="-". 4. Then you need to repeat the symbols in the string 20 times, code: s1=s*20. 5. Enter the print output code, code: print(s1). 6. Finally run the script and you will see our return value at the bottom: - repeated 20 times.

Detailed explanation of the method of converting int type to string in PHP In PHP development, we often encounter the need to convert int type to string type. This conversion can be achieved in a variety of ways. This article will introduce several common methods in detail, with specific code examples to help readers better understand. 1. Use PHP’s built-in function strval(). PHP provides a built-in function strval() that can convert variables of different types into string types. When we need to convert int type to string type,

The difference between Go language methods and functions lies in their association with structures: methods are associated with structures and are used to operate structure data or methods; functions are independent of types and are used to perform general operations.

Age spots are a common skin problem that tend to appear on the faces of older adults on exposed areas such as the neck and back of hands. It will also make people look older. It not only brings trouble to people's appearance. With the advancement of science and technology and the development of beauty technology, there are now many simple and quick ways to remove age spots, however. To restore young and healthy skin and help you eliminate age spots quickly, this article will share some effective skin care tips. 1. The importance of actively protecting the skin from ultraviolet damage, so it is very important to avoid prolonged exposure to the sun. Ultraviolet rays are one of the main causes of age spots. 2. Choose skin care products reasonably and use products containing antioxidants and whitening ingredients. Skin care products containing antioxidants and whitening ingredients can help reduce the appearance of age spots.
