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
Lodash Function | Bytes minified and gzipped | |
---|---|---|
isNil.gz | 496 | |
isNull.gz | 496 | |
isObjectLike.gz | 499 | |
isUndefined.gz | 499 | |
defaultTo.gz | 501 | |
eq.gz | 502 | |
head.gz | 505 | |
isObject.gz | 509 | |
first.gz | 512 |
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
// native | |
array.map(item => item.field) | |
array.filter(item => item.value === 1) | |
array.sort(item => item.value) | |
// lodash's shorter variants | |
_.map(array, "field") | |
_.filter(array, { value: 1 }) | |
_.sort(array, "value") |
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
// native variant needs to protect against null values | |
const data = null | |
const result = Array.isArray(data) ? data.map(item => parseInt(item) : [] | |
// not so with lodash | |
const result = _.map(null, item => parseInt(item)) | |
// no exception, result is [] | |
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
// native variant | |
const objectValues = Object.values(dataObject) | |
const result = objectValues.map(item => parseInt(item)) | |
// same as this lodash variant with implict conversion | |
const result = _.map(dataObject, item => parseInt(item)) |
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
import { chain, filter, map, sortBy, uniqBy, compact } from "lodash"; | |
const messageList = [{ user: 1, messageId: 1, creationDate: new Date(), text: "Hello" }]; | |
chain(messageList) | |
.filter({ user: 1 }) | |
.sortBy("creationDate") | |
.uniqBy("messageId") | |
.map("text") | |
.compact() |
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
import { uniqBy } from "lodash" | |
uniqBy(someCollection, "value") |
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
const prepareArray = (arr, field) => | |
if Array.isArray(arr) { | |
return arr.reduce((res, cur) => (res.length === 0 || cur[field] !== res[res.length - 1][field] ? [...res, cur] : res), []) | |
} | |
return [] | |
} | |
prepareArray(someDataCollection, "value") |