📜  Lodash _.explode() 方法

📅  最后修改于: 2022-05-13 01:56:55.559000             🧑  作者: Mango

Lodash _.explode() 方法

Lodash _.explode() 方法用于将字符串分解为字符数组。

句法:

_.explode( String );

参数:此方法采用单个参数,如上所述,如下所述:

  • String:这个方法需要一个String来爆炸。

返回值:该方法返回生成的字符数组。

注意:这在普通 JavaScript 中不起作用,因为它需要安装 lodash.js contrib 库。 Lodash.js contrib 库可以使用npm install lodash-contrib –save 安装

示例 1:

Javascript
// Defining lodash contrib variable 
var _ = require('lodash-contrib'); 
  
var a  = _.explode( "GeeksforGeeks" ); 
    
console.log("The generated array is : ", a);


Javascript
// Defining lodash contrib variable 
var _ = require('lodash-contrib'); 
  
var a  = _.explode( "Computer Science Portal" ); 
    
console.log("The generated array is : ", a);


输出:

The generated array is :
["G", "e", "e", "k", "s", "f", "o", "r", "G", "e", "e", "k", "s"]

示例 2:

Javascript

// Defining lodash contrib variable 
var _ = require('lodash-contrib'); 
  
var a  = _.explode( "Computer Science Portal" ); 
    
console.log("The generated array is : ", a);

输出:

The generated array is : [
    "C", "o", "m", "p", "u", "t", "e", "r", 
    " ", "S", "c", "i", "e", "n", "c", "e", 
    " ", "P", "o", "r", "t", "a", "l"
]