本文整理汇总了TypeScript中lodash.topath类的典型用法代码示例。如果您正苦于以下问题:TypeScript topath类的具体用法?TypeScript topath怎么用?TypeScript topath使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了topath类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: getIn
export function getIn(
obj: any,
key: string | string[],
def?: any,
p: number = 0
) {
const path = toPath(key);
while (obj && p < path.length) {
obj = obj[path[p++]];
}
return obj === undefined ? def : obj;
}
示例2: setIn
export function setIn(obj: any, path: string, value: any): any {
let res: any = {};
let resVal: any = res;
let i = 0;
let pathArray = toPath(path);
for (; i < pathArray.length - 1; i++) {
const currentPath: string = pathArray[i];
let currentObj: any = getIn(obj, pathArray.slice(0, i + 1));
if (resVal[currentPath]) {
resVal = resVal[currentPath];
} else if (currentObj) {
resVal = resVal[currentPath] = cloneDeep(currentObj);
} else {
const nextPath: string = pathArray[i + 1];
resVal = resVal[currentPath] =
isInteger(nextPath) && Number(nextPath) >= 0 ? [] : {};
}
}
resVal[pathArray[i]] = value;
return { ...obj, ...res };
}