Lodash Cheatsheet - Utility Functions
All essential Lodash commands organized by use case, with 44+ entries you can copy and run directly. Find the right command fast when you need it.
Back to LanguagesArray 7
_.map(arr, (n) => n * 2)map为新array
_.filter(arr, (n) => n > 0)Filter符合condition的元素
_.reduce(arr, (sum, n) => sum + n, 0)归约accumulate
_.chunk(arr, 2)每 2 个一组切分
_.flattenDeep([1, [2, [3]]])深度flatten
_.uniq([1, 2, 1, 3])arraydedupe
_.union([1, 2], [2, 3])多array并集dedupe
Object 7
_.get(obj, "a.b.c", "default")安全Get嵌套property
_.set(obj, "a.b.c", value)安全Set嵌套property
_.pick(obj, ["name", "age"])选取指定property
_.omit(obj, ["password"])排除指定property
_.merge(target, source)深度mergeobject
_.cloneDeep(obj)深拷贝
_.keys(obj) / _.values(obj)取键名/键值array
String 6
_.camelCase("Foo Bar")转驼峰命名
_.kebabCase("FooBar")转短横线命名
_.template("<%= name %>")({ name: "Vue" })Compiletemplatestring
_.escape("<a>")HTML 转义
_.trim(" abc ")去除首尾空白
_.pad("abc", 8)两端填充到指定length
Collection 6
_.groupBy(users, "age")按propertygroup
_.sortBy(users, ["age", "name"])多propertysort
_.countBy([1, 2, 2, 3], Math.floor)按规则计数
_.keyBy(users, "id")按property转为objectmap
_.sampleSize(arr, 2)随机抽取 n 个元素
_.shuffle(arr)随机打乱order
Functions 6
_.debounce(fn, 300)防抖(Stop触发后才Execute)
_.throttle(fn, 300)节流(固定频率Execute)
_.memoize(fn)cachefunction结果
_.once(fn)只Execute一次
_.partial(fn, _, 2)部分应用Options
_.bind(fn, thisArg, arg1)绑定 this 与Options
Number & Math 5
_.random(0, 100)生成 0-100 随机整数
_.range(0, 5)生成 [0, 1, 2, 3, 4]
_.clamp(num, 0, 100)限制在区间内
_.inRange(num, 0, 100)判断是否在区间内
_.round(1.234, 2)保留 2 位小数
Utils 7
_.isEmpty(value)判断是否is empty
_.isEqual(a, b)深度相等compare
_.isNil(value)判断 null 或 undefined
_.isArray(value) / _.isObject(value)type判断
_.has(obj, "a.b")check any/allproperty路径
_.defaults({ a: 1 }, { a: 0, b: 2 })填充未定义property
_.noop()空function占位
💡 Tips
- lodash-es 是 ES Module 版本,supports Tree Shaking,打package体积更小。
- _.get 和 _.set 可以安全操作深层嵌套object,避免 undefined 报错。
- debounce 适合SearchInput框,throttle 适合滚动/resize 事件。
- 现代 JS 可以用原生method替代部分 Lodash,如 Array.prototype.flat()、Object.keys()。
- 按需引入单个method import cloneDeep from "lodash/cloneDeep" 可进一步减小体积。
Official References
Commands are compiled from the official docs below. Click to verify the latest usage.
Maintained by LaoHand
Publicly updated on Jul 21, 2026, continuously proofread against official docs.
Found an error? Report it
Wrong command or description? Open an issue to help us fix it.
Found an error? Report it