当前位置:网站首页>In JS, string and array are converted to each other (I) -- the method of converting string into array

In JS, string and array are converted to each other (I) -- the method of converting string into array

2022-07-06 21:11:00 viceen

js in , String and array conversion ( One )—— The method of converting a string into an array

example

const string = 'uixdk';
 
// 1.  Use String.prototype.split() Method  
string.split('');
 
// 2.  Use ES6 Deconstruction operator 
[...string]; 
 
// 3.  Use Array.form()
Array.from(string);
 
// 4.  Use Object.assign() 
Object.assign([], string);
 
// The results are all ["u", "i", "x", "d", "k"]

Method 1、 Use split() Method ——( recommend )

split() Method is used to split a string into an array of strings , This method uses the specified separator provided in the parameter to separate it into substrings .

str.split(separator, limit)

Parameters :

  • separator Optional . String or regular expression , Split from where specified by this parameter string Object.
  • limit Optional . This parameter specifies the maximum length of the returned array . If this parameter is set , No more substrings will be returned than the array specified by this parameter . If the parameter is not set , The entire string will be split , Regardless of its length .
1.1、 General usage

Use common characters , for example @ or , Etc. as separator

var str =" Beijing @ The Beijing municipal @ Haidian District @ Xueyuan Road ";
var splitAdd = str.split("@");
console.log(splitAdd) // [" Beijing ", " The Beijing municipal ", " Haidian District ", " Xueyuan Road "]
1.2、 Separator that needs escape

When using * ^ : | . \ etc. 6 When a symbol is used as a separator , Above 6 Symbol escape characters , Must add "", namely split(“^”) etc. .

var str1 =" Beijing * The Beijing municipal * Haidian District * Xueyuan Road ";
var splitAdd1 = str1.split("\*"); 
console.log(splitAdd1)  // [" Beijing ", " The Beijing municipal ", " Haidian District ", " Xueyuan Road "]
1.3、 Is an empty string

1

var str =" welcome to beijing ";
var splitAdd3 = str.split("");
console.log(splitAdd3)  // [' north ', ' Beijing ', ' huan ', ' To meet ', ' you ']

2

var str="Welcome to here";
var n=str.split(" ");
console.log(n);  // ['Welcome', 'to', 'here']

Method 2、 Use ES6 Extension operator

  • sentence :[...string]
var str =" welcome to beijing ";
console.log([...str]);  // [' north ', ' Beijing ', ' huan ', ' To meet ', ' you ']

Method 3、 Use Array.from() Method ——( recommend )

Array.from() The method is javascript A built-in function in , It creates a new array instance from a given array .

For strings , Each alphabet of the string is converted to the elements of the new array instance ;

For integer values , New array instance simple Will get the elements of the given array .

  • grammar :Array.from(str)
var str =" welcome to beijing ";
console.log(Array.from(str));  // [' north ', ' Beijing ', ' huan ', ' To meet ', ' you ']

Method 4、 Use “Object.assign([], string)” sentence

var str =" welcome to beijing ";
console.log(Object.assign([], str));   // [' north ', ' Beijing ', ' huan ', ' To meet ', ' you ']
原网站

版权声明
本文为[viceen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061250443995.html