Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ JavaScript common array manipulation functions and usage     - Lambda expressions of evolution (Programming)

- The user of fedora is not in the sudoers file method to solve (Linux)

- AIX system file security aspects (Linux)

- LVM management parameters commonly used commands explained in detail (Linux)

- Linux SSH login without a password (Linux)

- Repair after installing Ubuntu no boot device error (Linux)

- Ubuntu and derivatives installation Atom 0.104.0 (Linux)

- Linux System Getting Started Tutorial: How to automatically set the JAVA_HOME environment variable on Linux (Linux)

- Django how to generate content in non-HTML formats (Programming)

- Linux boot process (Linux)

- Linux system Perl Lite netstat (Linux)

- To install Git on Ubuntu systems (Server)

- Talk Packages (Linux)

- Bash job control (Linux)

- C ++ hash function (Programming)

- The official release method to upgrade to Ubuntu 15.04 (Linux)

- ogg start being given libnnz11.so: can not open shared object file (Database)

- Java eight new features 8 (Programming)

- The top command under Linux (Linux)

- CentOS 6 adds disk quota limit (Linux)

  JavaScript common array manipulation functions and usage
  Add Date : 2016-12-24      
  Participated in the recent tip intern recruitment written test, we found that many questions will test pen to manipulate strings, say where the network pen questions, Taobao pen questions like. If you often take a written test or is someone who has, I believe you are like me, I found string handling front-end recruitment process of the most common questions. These questions have a characteristic standing examiner's point of view to consider, it's not a test you will not, but you can not borrow XX XX guide or manual re Baidu or Google's case, with a relatively simple way to write the answer to. Unfortunately, many developers, of course, I was one of them, for many frequently used string manipulation functions can not keep in mind their usage, the total had to resort to manual or XX XX Guide again or Baidu Google . And this result is that these very crucial basics, but not strong enough ram, had to use N layers of nested for loop to traverse one by one in the face of these questions. This is a signal, when you find that you're doing this type of problem when used for loop too much, so pay attention, chances are you've written way off. Do not underestimate these things, chances to play a big role in finding a job and normal development. Well, not much to say, we summarize one by one. It is inevitable omissions, if you happen to be found, please add or private letter.

1, string conversion

-------------------------------------------------- ------------------------------

String conversion is the most basic requirements and work, you can put any type of data are converted to a string, you can use any of the following three methods:

var num = 19; // 19

var myStr = num.toString (); // "19"

You can also do this:

var num = 19; // 19

var myStr = String (num); // "19"

Or, again something simple:

var num = 19; // 19

var myStr = "" + num; // "19"

2, the string split

-------------------------------------------------- ------------------------------


String split is about a string into a plurality of strings, JavaScript provides us in a very convenient function, such as:

var myStr = "I, Love, You, Do, you, love, me";

var substrArray = myStr .split ( ","); // [ "I", "Love", "You", "Do", "you", "love", "me"];

var arrayLimited = myStr .split ( ",", 3); // [ "I", "Love", "You"];

split () second parameter indicates the maximum length of the returned string array.

3. Get string length

-------------------------------------------------- ------------------------------

String length is often used in the development of very simple as follows:

var myStr = "I, Love, You, Do, you, love, me";

var myStrLength = myStr.length; // 25

4, the query substring

-------------------------------------------------- ------------------------------

Many people will forget these methods comes with JavaScript, or forget their specific usage, resulting in the title do have the time to do a nested for loop.

The first function: indexOf (), it begins searching at the beginning of the string, returns to find the corresponding coordinates, find returns -1. as follows:

var myStr = "I, Love, you, Do, you, love, me";

var index = myStr.indexOf ( "you"); // 7, based on 0, -1 not found

The second function: lastIndexOf (), it starts from the end of the string to find, find returns the corresponding coordinates, find returns -1. as follows:

var myStr = "I, Love, you, Do, you, love, me";

var index = myStr.lastIndexOf ( "you"); // 14

These two functions are equally receive the second optional parameter indicates the start to find the location.

5, the replacement string

-------------------------------------------------- ------------------------------

Just found strings should also not stop, general topics are also often encountered lets you find and replace with your own string, for example:

var myStr = "I, love, you, Do, you, love, me";

var replacedStr = myStr.replace ( "love", "hate"); // "I, hate, you, Do, you, love, me"

Replace default only to find the first time, you want to replace the global, it needs to be set on a regular global identity, such as:

var myStr = "I, love, you, Do, you, love, me";

var replacedStr = myStr.replace (/ love / g, "hate"); // "I, hate, you, Do, you, hate, me"

More Detailed Explanation may refer to: http: //www.w3school.com.cn/jsref/jsref_replace.asp

6, find the location of a given character or character encoding value

-------------------------------------------------- ------------------------------

Want to find the character given location, you can use the following functions:

var myStr = "I, love, you, Do, you, love, me";

var theChar = myStr.charAt (8); // "o", also starting from 0

Likewise, it's function is to find a brother character encoding value corresponding to the position, such as:

var myStr = "I, love, you, Do, you, love, me";

var theChar = myStr.charCodeAt (8); // 111

7, string concatenation

-------------------------------------------------- ------------------------------

 String concatenation operator can use a simple addition to the operator to get, such as:

var str1 = "I, love, you!";

var str2 = "Do, you, love, me?";

var str = str1 + str2 + "Yes!"; // "I, love, you Do, you, love, me Yes!?!"

Similarly, JavaScript also comes with related functions, such as:

var str1 = "I, love, you!";

var str2 = "Do, you, love, me?";

var str = str1.concat (str2); // "I, love, you Do, you, love, me!?"

Wherein the concat () function can have multiple arguments, pass multiple strings, stitching multiple strings.

8, cutting and extraction of strings

-------------------------------------------------- ------------------------------

 Three can be extracted from a string and cutting methods, such as:

First, using the slice ():

var myStr = "I, love, you, Do, you, love, me";

var subStr = myStr.slice (1,5); // ", lov"

Second, use substring ():

var myStr = "I, love, you, Do, you, love, me";

var subStr = myStr.substring (1,5); // ", lov"

Third, use substr ():

var myStr = "I, love, you, Do, you, love, me";

var subStr = myStr.substr (1,5); // ", love"

And the first and second difference is that, substr () string parameter represents the maximum length of the second interception, as the results shown.

 9, the string case conversion

-------------------------------------------------- ------------------------------

Commonly converted to uppercase or lowercase string functions, as follows:

var myStr = "I, love, you, Do, you, love, me";

var lowCaseStr = myStr.toLowerCase (); // "i, love, you, do, you, love, me";

var upCaseStr = myStr.toUpperCase (); // "I, LOVE, YOU, DO, YOU, LOVE, ME"

10, string matching

-------------------------------------------------- ------------------------------

You may need to string matching regular expressions have a certain understanding, take a look at match () function:

var myStr = "I, love, you, Do, you, love, me";

var pattern = / love /;

var result = myStr.match (pattern); // [ "love"]

console.log (result .index); // 2

console.log (result.input); // I, love, you, Do, you, love, me

As you can see, match () function call on a string, and accept a regular parameter. Look at the second example, the use of exec () function:

var myStr = "I, love, you, Do, you, love, me";

var pattern = / love /;

var result = pattern .exec (myStr); // [ "love"]

console.log (result .index); // 2

console.log (result.input); // I, love, you, Do, you, love, me

It is simple, just the regular and strings changed positions, namely exec () function is called on the positive, the parameter string is passed. For the above two methods, the results are returned match the success of the first matching string if the match failed to return null.

Let's look at a similar method search (), such as:

var myStr = "I, love, you, Do, you, love, me";

var pattern = / love /;

var result = myStr.search (pattern); // 2

Returns only found matching index, or -1 if the match fails.

11, string comparison

-------------------------------------------------- ------------------------------

Compare two strings, comparisons are rules in accordance with the alphabetical order of the comparison, such as:

var myStr = "chicken";

var myStrTwo = "egg";

var first = myStr.localeCompare (myStrTwo); // -1

first = myStr.localeCompare ( "chicken"); // 0

first = myStr.localeCompare ( "apple"); // 1

12, for example

-------------------------------------------------- ------------------------------

Finally, we look at the front end of a pen questions, where to network, I believe many children have done this over the subject. Title: Write a getSuffix function for obtaining input parameter name suffix, such as input abcd.txt, return txt. I enclose my answer:

function getSuffix (file) {

      return file.slice (file.lastIndexOf () + 1, file.length ".");



-------------------------------------------------- ------------------------------

I believe in JavaScript string manipulation functions should be more than these, but these should be listed above are very common. What if there is need to add, please add! I would like to see these in the future, to face face questions written string you can be very calm in the face.

Summarized under the common JavaScript string manipulation functions and usage. Today there is just time, but also to the common JavaScript array manipulation functions and usage summarize, so to facilitate ready reference. If you happen to also prepare a variety of written, I hope for your help. They are also welcome to add.

1 array creation

-------------------------------------------------- ------------------------------

Create an array should be the most simple and useful array literal to create and array constructors two methods below:

var array1 = new Array ();

var array2 = [];

The above are the most common two ways to create an array, wherein the second method is straightforward because the developer respected. Among them, create an array using the constructor can also be used to receive the value stored in the array, such as:

var array3 = new Array ( "num1", "num2", "num3"); // [ "num1", "num2", "num3"]

Similarly, if you pass the constructor value is a number, if the number is greater than 0, the array is the length of this newly created, such as:

var array4 = new Array (5);

array4.length; // 5

Under the same results, we recommend using literal form create an array.

2, the array detector

-------------------------------------------------- ------------------------------

There are two ways to detect the array, the first use instanceof, as follows:

var array5 = [];

array5 instanceof Array; // true

Meanwhile, Array also comes isArray () method, as follows:

var array6 = [];

Array.isArray (array6); // true

3. Get array length

-------------------------------------------------- ------------------------------

Array length by .length acquisition, as follows:

var array7 = [1,2,3,4];

array7.length; // 4

Meanwhile, the length of the array you can always set up, then what follows will be automatically added dimension undefined length If more than before, otherwise it will intercept the contents of the effective length, as follows:

var array8 = [1,2,3,4,5];

console.log (array8.length); // 5

array8.length = 8;

array8; // [1, 2, 3, 4, 5, undefined x 3]


array8.length = 3;

array8; // [1,2,3]

4. Gets or sets an array of values

-------------------------------------------------- ------------------------------

We can get and set the value of the array, to remember here is that the array index is starting from zero, as follows:

var array9 = [1,2,3,4,5,6];

array9 [2]; // 3

array9 [2] = 4;

array9 [2]; // 4

5, an array of string conversion

-------------------------------------------------- ------------------------------

You can call the array to a string array of built-in toString () method that returns a string array, as follows:

var array10 = [1,2,3,4];

array10.toString (); // "1,2,3,4"

In addition, the array also has another very useful function join (), which takes a string parameter that is interposed between the array of strings is formed as follows:

var array11 = [1,2,3,4];

array11.join ( "||"); // "1 || 2 || 3 || 4"

6, an array of add and delete items

-------------------------------------------------- ------------------------------

Recall that we can set the length to the array automatically add some undefined value. At the same time, we can also access an array of numerical subscript exceeds the original length of the array in order to add items, such as:

var array12 = [1,2,3,4];

array12 [5] = 5;

array12 [6] = 6;

array12; // [1, 2, 3, 4, undefined x 1, 5, 6]

In addition, we can also push () to add a new element to the array, as follows:

var array13 = [1,2,3,4];

array13.push (5,6);

array13; // [1, 2, 3, 4, 5, 6]

And push () is the opposite of a pop () method to delete the items in the array, and the last item from the array, for example:

var array14 = [1,2,3,4];

array14.pop (); // 4

array14.pop (); // 3;

array14; // [1, 2]

At the same time, we can delete to delete an array, but only removes that value, restore the default undefined, as follows:

var array15 = [1,2,3,4,5];

delete array15 [1];

array15; // [1, undefined x 1, 3, 4, 5]

Two other very useful way shift () and unshift (), with the push () and pop () method is similar, wherein, shift () method for starting to remove elements from an array and returns the starting position of the element to be deleted, as follows :

var array16 = [1,2,4,5,6];

array16.shift (); // 1

array16; [2,4,5,6]

The unshift () and shift () Instead, to add to the beginning of the array elements, and the elements of the original array after each shift, as follows:

var array17 = [1,2,3,4];

array17.unshift (2); // returns an array of length 5, the same

array17.unshift (3);

array17.unshift (4);

array17; // [4, 3, 2, 1, 2, 3, 4]

7, flip, and sort arrays

-------------------------------------------------- ------------------------------

We can flip an array, but also allows to sort an array, as follows:

var array18 = [21,14,54,35,23,44,103];

array18.reverse (); // [103, 44, 23, 35, 54, 14, 21]

array18.sort (); // [103, 14, 21, 23, 35, 44, 54]

Note that the array size is not the default sort, but in accordance with the corresponding encoded string individually sorted. You can give a sort () is passed a comparison function, change the rules of the class, as follows:

var array18 = [21,14,54,35,23,44,103];

array18.reverse (); // [103, 44, 23, 35, 54, 14, 21]

array18.sort (function (a, b) {return a - b;}); // [14, 21, 23, 35, 44, 54, 103]

8, an array of connection

-------------------------------------------------- ------------------------------

We can connect together different array, as follows:

var array19 = [1,2,3];

var array20 = [4,5];

var array21 = array19.concat (array20); // [1, 2, 3, 4, 5]

Note that the original array are not and will not change.

9, of an array

-------------------------------------------------- ------------------------------

We can also () the array divided by the slice, which accepts one parameter representing the starting position, then split, accept a second optional parameter indicates the position of the end, as follows:

var array22 = [1,2,3,4,5,6];

var array23 = array22.slice (4); // 5,6

var array24 = array22.slice (2,4); // 3,4

Note that, segmented array contains slice () does not contain the starting position of the end position.

10, the most powerful of the splice ()

-------------------------------------------------- ------------------------------

Now, talk about the most powerful splice () function, and it accepts three parameters, allows users to add, delete, modify different functions. Look on w3school its syntax is as follows:

1) add an element

var array25 = [1,2,3,4,5,6];

array25.splice (2,0,88,77); // [] returns the element is removed, there is no delete, returns null

array25; // [1, 2, 88, 77, 3, 4, 5, 6]

2) modify elements

var array26 = [1,2,3,4,5,6,7];

array26.splice (2,2,33,44); // [3,4]

array26; // [1, 2, 33, 44, 5, 6, 7]

3) Removing elements

var array27 = [1,2,3,4,5,6,7];

array27.splice (2,2); // [3, 4]

array27; // [1, 2, 5, 6, 7]


-------------------------------------------------- ------------------------------

Hastily summed up, I do not know if I have a function or leave out any key, and I hope we can give added!
- Several SQL Server data migration / Export Import Practice (Database)
- Hadoop safe hftp (Server)
- Sudo and Root Account in Ubuntu related (Linux)
- 10 tips daily Docker (Server)
- ApacheDS configuration of users and user groups to achieve SSO (Server)
- When Vim create Python scripts, vim autocomplete interpreter and encoding method (Programming)
- DIY security of Linux platform (Linux)
- Three methods easy data encryption on Linux (Linux)
- shellinabox: one uses AJAX Web-based terminal emulator (Linux)
- Windows SmartGit installation (Linux)
- Android components series Activity (Programming)
- Linux terminal interface font color settings (Linux)
- Android in the coordinate system and a method to obtain the coordinates (Programming)
- Orionode source Linux Mint installation (Linux)
- Git uses a basic tutorial (Linux)
- Use GLOBK batch command network adjustment (Linux)
- Reset CentOS / RHEL root account password 7 (Linux)
- Talking about the shortcomings and deficiencies of the firewall (Linux)
- In-depth understanding of capture Node.js garbage collection and memory leaks (Programming)
- Ubuntu 14.04 install PostgreSQL 9.2 (Database)
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.