This article lists 21 Javascript tips worth adding to your favorites. In actual work, if you can use them properly, it will greatly improve your work efficiency.
1. Convert Javascript array to CSV format
First, consider the following application scenario. There is a Javscript array of numeric type (or numeric type). Now you need to convert it to a CSV format file separated by commas. The following tips can be used:
The Code is as follows:
Var fruits = ['apple', 'peaches', 'oranges', 'mangoes'];
Var str = fruits. valueOf ();
Output: apple, peaches, oranges, mangoes
The valueOf () method converts the Javascript array into a comma-separated string. Note that if you do not want to use commas (,) to separate them, for example, using the | Number, use the join method as follows:
The Code is as follows:
Var fruits = ['apple', 'peaches', 'oranges', 'mangoes'];
Var str = fruits. join ("| ");
Output: apple | peaches | oranges | mangoes
2. Convert the CSV format back to the Javscript array.
So how to convert a CSV string back to a Javascript array? You can use the split () method to separate all specified characters. The Code is as follows:
The Code is as follows:
Var str = "apple, peaches, oranges, mangoes ";
Var fruitsArray = str. split (",");
Output fruitsArray [0]: apple
3. remove an element from the Array Based on the index
If you need to remove an element from the Javascript array, you can use the splice method, which will be based on the input parameter n, remove the nth element from the array (calculated from the 0th bits in the Javascript array ).
The Code is as follows:
Function removeByIndex (arr, index ){
Arr. splice (index, 1 );
}
Test = new Array ();
Test [0] = 'apple ';
Test [1] = 'ball ';
Test [2] = 'cat ';
Test [3] = 'Dog ';
Alert ("Array before removing elements:" + test );
RemoveByIndex (test, 2 );
Alert ("Array after removing elements:" + test );
The final output is Apple, Ball, and Dog.
4. Remove values from array elements based on the element values.
The following is a practical technique to delete elements in an Array Based on the given values. The Code is as follows:
The Code is as follows:
Function removeByValue (arr, val ){
For (var I = 0; I if (arr [I] = val ){
Arr. splice (I, 1 );
Break;
}
}
}
Var somearray = ["mon", "tue", "wed", "thur"]
RemoveByValue (somearray, "tue ");
// Somearray will have "mon", "wed", "thur"
Of course, the better way is to use prototype to implement it, as shown in the following code:
The Code is as follows:
Array. prototype. removeByValue = function (val ){
For (var I = 0; I If (this [I] = val ){
This. splice (I, 1 );
Break;
}
}
}
//..
Var somearray = ["mon", "tue", "wed", "thur"]
Somearray. removeByValue ("tue ");
5. Call a method dynamically using the method specified by the string
Sometimes, you need to dynamically call an existing method at runtime and input parameters for it. How can this be achieved? The following code can be used:
The Code is as follows:
Var strFun = "someFunction"; // someFunction is the defined method name.
Var strParam = "this is the parameter"; // parameters of the method to be passed in
Var fn = window [strFun];
// Call method input parameters
Fn (strParam );
6. Generate a random number ranging from 1 to N.
The Code is as follows:
Var random = Math. floor (Math. random () * N + 1 );
// Generate a random number between 1 and 10
Var random = Math. floor (Math. random () * 10 + 1 );
// Generate a random number between 1 and 100
Var random = Math. floor (Math. random () * 100 + 1 );
7. Capture events closed by the browser
We often hope that when users close the browser and prompt users to save unsaved items, the following Javascript technique is very useful. The Code is as follows:
The Code is as follows: