_.zipObjectDeep([props=[]], [values=[]])

63

_.zipObjectDeep([props=[]], [values=[]])

_.zipObjectDeep和zipObject类似,区别是支持属性路径

参数

[props=[]] (Array): 属性标识符
[values=[]] (Array): 属性值

返回值

(Array): 返回新对象

例子

_.zipObjectDeep(['a.b[0].c', 'a.b[1].d'], [1, 2]);
// => { 'a': { 'b': [{ 'c': 1 }, { 'd': 2 }] } }

源代码:

import baseSet from './.internal/baseSet.js'
import baseZipObject from './.internal/baseZipObject.js'

/**
 * This method is like `zipObject` except that it supports property paths.
 *
 * @since 4.1.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, zipObject, zipWith
 * @example
 *
 * zipObjectDeep(['a.b[0].c', 'a.b[1].d'], [1, 2])
 * // => { 'a': { 'b': [{ 'c': 1 }, { 'd': 2 }] } }
 */
//和zipObject类似,区别是支持属性路径
function zipObjectDeep(props, values) {
  return baseZipObject(props || [], values || [], baseSet)
  //调用baseZipObject处理
}

export default zipObjectDeep
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

baseSet

import assignValue from './assignValue.js'
import castPath from './castPath.js'
import isIndex from './isIndex.js'
import isObject from '../isObject.js'
import toKey from './toKey.js'

/**
 * The base implementation of `set`.
 *
 * @private
 * @param {Object} object The object to modify.
 * @param {Array|string} path The path of the property to set.
 * @param {*} value The value to set.
 * @param {Function} [customizer] The function to customize path creation.
 * @returns {Object} Returns `object`.
 */
//set方法的基础实现
//object需要修改的对象
//path属性路径
//value需要设置的value
//customizer自定义处理属性路径的方法
function baseSet(object, path, value, customizer) {
  if (!isObject(object)) {//如果object不是对象,返回原值
    return object
  }
  path = castPath(path, object)//将path路径字符串变成一个路径数组

  const length = path.length//路径数组的长度
  const lastIndex = length - 1//路径数组的最后一个索引

  let index = -1//循环索引
  let nested = object

  while (nested != null && ++index < length) {//object不为空,并且路径数组没有循环完
    const key = toKey(path[index])//将路径数组当前路径变成合法键名
    let newValue = value

    if (index != lastIndex) {//如果没有循环到path数组最后一个
      const objValue = nested[key]//当前路径从object中取到的值
      newValue = customizer ? customizer(objValue, key, nested) : undefined//用customizer计算出新值
      if (newValue === undefined) {//如果customizer计算出的新值是undefined,新值就赋值为直接用路径在object取到的值
        newValue = isObject(objValue)
          ? objValue
          : (isIndex(path[index + 1]) ? [] : {})
      }
    }
    assignValue(nested, key, newValue)//调用assignValue分配新值到object上的指定路径上
    nested = nested[key]//下一次深层循环的object值
  }
  return object
}

export default baseSet

 

posted @ 2018-10-27 00:05  hahazexia  阅读(420)  评论(0)    收藏  举报