-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import has from './has' | ||
import isObject from './isObject' | ||
import toPath from './toPath' | ||
|
||
/** | ||
* 根据 obj 对象的路径 path 设置值。 | ||
* | ||
* @param obj 要检索的对象 | ||
* @param path 属性路径 | ||
* @param value 要设置的值 | ||
*/ | ||
export default function set(obj: object, path: string | string[], value: any): void { | ||
path = Array.isArray(path) ? path : toPath(path) | ||
let last: any = obj | ||
for (let i = 0, len = path.length; i < len; i++) { | ||
if (i === len - 1) { | ||
last[path[i]] = value | ||
} else { | ||
last = last[path[i]] = isObject(last[path[i]]) ? last[path[i]] : {} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters