_.zipObject([props=[]], [values=[]])
62
_.zipObject([props=[]], [values=[]])
_.zipObject类似fromPairs方法,接收两个数组参数,一个数组是属性标识,一个是相对应的属性值
参数
[props=[]] (Array): 属性标识符
[values=[]] (Array): 属性值
返回值
(Array): 返回新对象
例子
_.zipObject(['a', 'b'], [1, 2]); // => { 'a': 1, 'b': 2 }
源代码:
import assignValue from './.internal/assignValue.js' import baseZipObject from './.internal/baseZipObject.js' /** * This method is like `fromPairs` except that it accepts two arrays, * one of property identifiers and one of corresponding values. * * @since 0.4.0 * @category Array * @param {Array} [props=[]] The property identifiers. * @param {Array} [values=[]] The property values. * @returns {Object} Returns the new object. * @see unzip, unzipWith, zip, zipObjectDeep, zipWith * @example * * zipObject(['a', 'b'], [1, 2]) * // => { 'a': 1, 'b': 2 } */ //类似fromPairs方法,接收两个数组参数,一个数组是属性标识,一个是相对应的属性值 function zipObject(props, values) { return baseZipObject(props || [], values || [], assignValue) //调用vaseZipObject处理 } export default zipObject
baseZipObject
/** * This base implementation of `zipObject` which assigns values using `assignFunc`. * * @private * @param {Array} props The property identifiers. * @param {Array} values The property values. * @param {Function} assignFunc The function to assign values. * @returns {Object} Returns the new object. */ //zipObject的基础实现,使用assignFunc方法来分配value function baseZipObject(props, values, assignFunc) { let index = -1//循环索引 const length = props.length//key组成的数组长度 const valsLength = values.length//value组成的数组长度 const result = {}//结果对象 while (++index < length) { const value = index < valsLength ? values[index] : undefined//value值,如果values没有元素了,就赋值为undefined assignFunc(result, props[index], value)//调用assignFunc向结果对象添加新键值 } return result } export default baseZipObject
assignValue
import baseAssignValue from './baseAssignValue.js' import eq from '../eq.js' /** Used to check objects for own properties. */ const hasOwnProperty = Object.prototype.hasOwnProperty /** * Assigns `value` to `key` of `object` if the existing value is not equivalent. * * @private * @param {Object} object The object to modify. * @param {string} key The key of the property to assign. * @param {*} value The value to assign. */ //将value分配给object上的key,如果和已存在的value的值不一样 function assignValue(object, key, value) { const objValue = object[key]//object上原来的key对应的值 if (!(hasOwnProperty.call(object, key) && eq(objValue, value))) { //如果object没有key这个自身属性 或者objValue和value不相等 if (value !== 0 || (1 / value) == (1 / objValue)) { baseAssignValue(object, key, value) } } else if (value === undefined && !(key in object)) { //如果object自身和原型链上都没有key这个属性,并且value是undefined baseAssignValue(object, key, value) } } export default assignValue
baseAssignValue
/** * The base implementation of `assignValue` and `assignMergeValue` without * value checks. * * @private * @param {Object} object The object to modify. * @param {string} key The key of the property to assign. * @param {*} value The value to assign. */ //assignValue和assignMergeValue的基础实现 function baseAssignValue(object, key, value) { if (key == '__proto__') {//如果key是__proto__,就用Object.defineProperty设置 Object.defineProperty(object, key, { 'configurable': true, 'enumerable': true, 'value': value, 'writable': true }) } else {//否则直接设置 object[key] = value } } export default baseAssignValue