_.join(array, [separator=','])

24

_.join(array, [separator=','])
_.join将数组里所有的元素依次序连接成字符串并且包含分隔符
参数

array (Array): 需要转换的数组
[separator=','] (string): 数组元素之间的分隔符

返回值

(string): 返回连接好的字符串

例子

_.join(['a', 'b', 'c'], '~');
// => 'a~b~c'

源代码:

/**
 * lodash 4.0.1 (Custom Build) <https://lodash.com/>
 * Build: `lodash modularize exports="npm" -o ./`
 * Copyright 2012-2016 The Dojo Foundation <http://dojofoundation.org/>
 * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
 * Copyright 2009-2016 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
 * Available under MIT license <https://lodash.com/license>
 */

/** Used for built-in method references. */
var arrayProto = Array.prototype;

/* Built-in method references for those with the same name as other `lodash` methods. */
var nativeJoin = arrayProto.join;

/**
 * Converts all elements in `array` into a string separated by `separator`.
 *
 * @static
 * @memberOf _
 * @since 4.0.0
 * @category Array
 * @param {Array} array The array to convert.
 * @param {string} [separator=','] The element separator.
 * @returns {string} Returns the joined string.
 * @example
 *
 * _.join(['a', 'b', 'c'], '~');
 * // => 'a~b~c'
 */
//将数组里所有的元素依次序连接成字符串并且包含分隔符
function join(array, separator) {//Array.prototype.join通过call调用
  return array ? nativeJoin.call(array, separator) : '';
}

module.exports = join;

 

 

posted @ 2018-10-15 11:23  hahazexia  阅读(1302)  评论(0)    收藏  举报