20
loading...
This website collects cookies to deliver better user experience
let oneStooge = ["Moe"];
let twoStooges = ["Larry", "Curly"];
// Both create ["Moe", "Larry", "Curly"]
let threeStoogesLodash = _.concat(oneStooge, twoStooges);
let threeStoogesVanilla = [...oneStooge, ...twoStooges];
let someArray = [100, 99, 98, 97, 96, 95];
let someArrayCopy = [...someArray];
_.fill(someArray, 0, 2, 5); // [100, 99, 0, 0, 0, 95]
someArrayCopy.fill(0, 2, 5); // [100, 99, 0, 0, 0, 95]
let sonicCharacters = [
"Sonic",
"Tails",
"Knuckles",
["Amy Rose", "Shadow", "Dr. Eggman"]
];
// Both return:
// ["Sonic", "Tails", "Knuckles", "Amy Rose", "Shadow", "Dr. Eggman"]
let sonicCharactersFlatLodash = _.flatten(sonicCharacters);
let sonicCharactersFlatVanilla = sonicCharacters.flat();
let batmanLyrics = ["na", "na", "na", "na", "na", "Batman!"];
// Both return ["na", "Batman"]
let batmanLyricsUniqueLodash = _.uniq(batmanLyrics);
let batmanLyricsUniqueVanilla = [...new Set(batmanLyrics)];
let countries = [
"United States",
"Belgium",
"Japan",
"China",
"South Korea"
];
// Both return ["Japan", "China", "South Korea"]
let asianCountriesLodash = _.filter(
countries,
country => country != "United States" && country != "Belgium"
);
let asianCountriesVanilla = countries.filter(
country => country != "United States" && country != "Belgium"
);
let frenchFlagColors = ["Blue", "White", "Red"];
// Both return false
let frenchFlagHasGreenLodash = _.include(frenchFlagColors, "Green");
let frenchFlagHasGreenVanilla = frenchFlagColors.include("Green");
// In ES6 module style:
import map from "lodash/map";
import cloneDeep from "lodash/cloneDeep";
// Or their CommonJS counterparts:
const map = require("lodash/map");
const cloneDeep = require("lodash/cloneDeep");
npm i lodash.clonedeep --save
// In ES6 module style:
import cloneDeep from "lodash.clonedeep";
// Or their CommonJS counterpart:
const cloneDeep = require("lodash.clonedeep");