📜  Lodash _.toArray() 方法

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

Lodash _.toArray() 方法

Lodash 是一个基于 underscore.js 的 JavaScript 库。 Lodash 有助于处理数组、字符串、对象、数字等。

_.toArray()方法用于将给定值转换为数组。

句法:

_.toArray( value )

参数:此方法接受如上所述和如下所述的单个参数:

  • value:此参数保存要转换的值。

返回值:该方法返回转换后的数组。

示例 1:

Javascript
// Requiring the lodash library  
const _ = require("lodash");  
      
// Use of _.toArray() method 
console.log(_.toArray(null)); 
console.log(_.toArray('gfg'));


Javascript
// Requiring the lodash library  
const _ = require("lodash");  
      
// Use of _.toArray() method 
console.log(_.toArray(10)); 
console.log(_.toArray({ 'html': 1, 
    'css': 2, 'javascript': 3 }));


输出:

[]
['g', 'f', 'g']

示例 2:

Javascript

// Requiring the lodash library  
const _ = require("lodash");  
      
// Use of _.toArray() method 
console.log(_.toArray(10)); 
console.log(_.toArray({ 'html': 1, 
    'css': 2, 'javascript': 3 }));


输出:

[]
[1, 2, 3]