JS cold knowledge that you don't know (how many detours have you taken as a development engineer?)

toLocaleString()

The toLocaleString() method converts the Date object to a string according to the local time and returns the result.

new Date().toLocaleString() 默认显示的格式是 yyyy-mm-dd hh:mm:ss
new Date()的默认格式是  Thu Mar 31 2022 14:30:20 GMT+0800 (中国标准时间)

You can also convert numbers into thousandths format:

let num=12345678;
console.log(num.toLocaleString()); // 12,345,678

Time can be converted to 24-hour clock:

// 2021/12/12 下午7:39:06
console.log(new Date().toLocaleString() 

// 2021/12/12 19:39:06
console.log(new Date().toLocaleString('chinese',{
    
    hour12:false})) 

split()

Definition and Usage
The split() method is used to split a string into an array of strings.

Return Value
An array of strings. The array is created by splitting the string stringObject into substrings at the boundaries specified by the separator. The strings in the returned array do not include the separator itself.

However, if separator is a regular expression containing subexpressions, the returned array includes strings that match those subexpressions (but not text that matches the entire regular expression)

Example 1 :

<script type="text/javascript">

var str="How are you doing today?"
// 以空格为单位进行分割
document.write(str.split(" ") + "<br />")
// 以字符为单位进行分割
document.write(str.split("") + "<br />")
// 取第三个空格之前的内容并以空格进行分割
document.write(str.split(" ",3))

</script>

output

How,are,you,doing,today?
H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,?
How,are,you

Example 2
In this example, we will split a string with a more complex structure:

// 以:为单位分割
"2:3:4:5".split(":")	//将返回["2", "3", "4", "5"]
// 以 | 为单位分割
"|a|b|c".split("|")	//将返回["", "a", "b", "c"]

Example 3 Split sentences into words

var sentence = 'How are you?'
var word = sentence.split(' ')
console.log(word)
// (3) ['How', 'are', 'you?']
undefined
var sentence = 'How are you?'
var word = sentence.split(/\s+/)
console.log(word)
// (3) ['How', 'are', 'you?']

Example 4
If you wish to split words into letters, or strings into characters, use the following code:

"hello".split("")	//可返回 ["h", "e", "l", "l", "o"]

To return only a subset of characters, use the howmany parameter:

"hello".split("", 3)	//可返回 ["h", "e", "l"]

JavaScript Array reverse() 方法

Example
Reverse the order of elements in an array:

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.reverse();

Definition and Usage
The reverse() method reverses the order of elements in an array.

Note: The reverse() method will change the original array.

Little knowledge The reverse() method can not only be used in arrays but also reverse the order of strings

Guess you like

Origin blog.csdn.net/weixin_50001396/article/details/123871507