《其实你并不需要Lodash / Underscore》

You-Dont-Need-Lodash-Underscore
《其实你并不需要Lodash / Underscore》
  接触前端较晚,主要使用的是React+ES5/ES6,下面大部分在前端开发过程中接触过,就当是做一份ES5/ES6笔记:

一、Array数组

1、_.concat

将原数组拷贝一份副本, 拼接操作副本新数组,原数组不变

const array = [1]
const other = array.concat(2, [3], [[4]])

console.log(other); // [1, 2, 3, [4]]
console.log(array); // [1]
2、_.fill

修改替换数组中元素的值

const array = [1, 2, 3];
array.fill('a');
console.log(array); // ['a', 'a', 'a']

console.log(Array(3).fill(2)); // [2, 2, 2]

console.log([4, 6, 8, 10].fill('*', 2, 3)); // [4, '*', '*', 10]; 从下标为2开始到下标为3,第三个为空时默认替换到数组最尾
3、_.find

返回从数组中查找到符合条件的第一个值

const users = [
    { 'user': 'barney',  'age': 36, 'active': true },
    { 'user': 'fred',    'age': 40, 'active': false },
    { 'user': 'pebbles', 'age': 1,  'active': true }
  ];
const findOut = users.find(item => item.age < 40);
console.log(findOut); // { active: true, age: 36, user: "barney" }
4、_.findIndex

返回从数组中查找到符合条件的第一个值的下标,没有符合的返回-1

const users = [
    { 'user': 'barney',  'age': 36, 'active': true },
    { 'user': 'fred',    'age': 40, 'active': false },
    { 'user': 'pebbles', 'age': 1,  'active': true }
  ];
const findIndex = users.findIndex(item => item.age < 40);
console.log(findIndex); // 0
5、_.indexOf

返回从数组中查找到的第一个值的下标,没有符合的返回-1
Returns the first index at which a given element can be found in the array, or -1 if it is not present.

const array = [2, 9, 9];
const result = array.indexOf(9);
console.log(result); // 1
6、_.join

根据分隔符来拼接数组中元素

const result = ['one', 'two', 'three'].join('--');
console.log(result); // "one--two--three"
7、_.lastIndexOf

返回在数组中最后出现该值的index下标,没有返回-1

const array = [2, 9, 9, 4, 3, 6];
const result = array.lastIndexOf(9);
console.log(result); // 2
8、_.reverse

反转数组

const array = [1, 2, 3];
console.log(array.reverse()); // [3, 2, 1]

二、Collection集合

1、_.each

依次yielding数组元素, 可以在中间操作每个元素
Iterates over a list of elements, yielding each in turn to an iteratee function.

[1, 2, 3].forEach((value, index) => {
    console.log(value * 3);
}); // 3 6 9 依次输出
2、_.every

数组中每个元素是否符合提供的测试条件函数

const isLargerThanTen = (element, index, array) => {
   console.log(element, index, array);
   return element >= 10;
}

const array = [10, 20, 30];
const result = array.every(isLargerThanTen);
console.log(result); // true
3、_.filter

过滤出符合提供的条件函数的值, 并返回它们组成的数组

const isBigEnough = (value) => {
  return value >= 10
}
const array = [12, 5, 8, 130, 44];
const filtered = array.filter(isBigEnough);
console.log(filtered); // [12, 130, 44]
4、_.includes

该值是否在数组中

const array = [1, 2, 3];
array.includes(1); // true
// 用indexOf相同效果
const array = [1, 2, 3];
array.indexOf(1) > -1; // true
5、_.map

将数组中元素每个值改变后返回新数组

const array1 = [1, 2, 3];
const array2 = array1.map((value, index) => {
  return value * 2 + index;
});
console.log(array2); // [2, 5, 8]
6、_.pluck

pluck是Lodash v4.0之前的函数

const array1 = [{name: "Alice"}, {name: "Bob"}, {name: "Jeremy"}];
const names = array1.map(x => {
  return x.name;
})
console.log(names); // ["Alice", "Bob", "Jeremy"]
7、_.reduce

从左到右计算一个数组中各元素值

const array = [0, 1, 2, 3, 4];
const result = array.reduce((previousVal, currentVal, currentIndex, array) => {
  return previousVal + currentVal;
});
console.log(result); // 等于各元素之和10
8、_.reduceRight

从右到左计算一个数组中各元素值

const array = [0, 1, 2, 3, 4];
const result = array.reduceRight((previousVal, currentVal, currentIndex, array) => {
  return previousVal - currentVal;
});
console.log(result); // 从右减到左,等于-2
9、_.size

返回对象的键值对个数

const result2 = Object.keys({one: 1, two: 2, three: 3}).length;
console.log(result2); // 3
10、_.some

数组中是否有些元素符合条件

const isLargerThanTen = (element, index, array) => {
  return element >= 10;
}
const array = [10, 9, 8];
const result = array.some(isLargerThanTen);
console.log(result); // true

三、Function函数

1、_.after

【注】这只是一种可选的实现方式在执行之前保证所有async calls异步回调已结束

const notes = ['profile', 'settings'];
notes.forEach((note, index) => {
  console.log(note);
  if (notes.length === (index + 1)) {
    render(); // 到数组最后一个元素执行渲染方法
  }
});

四、Lang属性

1、_.isNaN

检测值是否是NaN

// ES6
console.log(Number.isNaN(NaN)); // true

五、Object对象

1、_.assign

将自身可enumerable属性加到目标对象

const result1 = Object.assign({1: 'aaa'}, {2: 'bbb'});
console.log(result1); // { 1: "aaa", 2: "bbb" }

const result2 = Object.assign({1: 'aaa'}, ['a', 'b', 'c']);
console.log(result2); // { 0: "a", 1: "b", 2: "c" }
2、_.keys

取出对象所有自身可enumerable属性

const result2 = Object.keys({one: 1, two: 2, three: 3});
console.log(result2);  // ["one", "two", "three"]

六、String字符串

1、_.repeat

重复字符串count次

const result = 'abc'.repeat(2);
console.log(result); // 'abcabc'
2、_.toLower

字符串中字符所有大写转成小写

const result = 'Fo1OBAR'.toLowerCase();
console.log(result); // "fo1obar"
3、_.toUpper

字符串中字符所有小写转成大写

const result = 'foobar'.toUpperCase();
console.log(result); // "FOOBAR"
4、_.trim

裁剪字符串首尾的空符

const result = ' abc '.trim()
console.log(result); // 'abc'

【拓展】

1、unshift向数组添加的第一个元素
const data = ['1', '2', '3'];
data.unshift('0');

console.log(data); // ["0", "1", "2", "3"]
2、Array.from();

creates a new Array instance from an array-like or iterable object.

Array.from({length: 5}, (v, index) => index); // [0, 1, 2, 3, 4]

Array.from([1, 2, 3], x => x + x); // [2, 4, 6]
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Collection Functions (Arrays or Objects) _.each(list, iterator, [context]) Alias: forEach Iterates over a list of elements, yielding each in turn to an iterator function. The iterator is bound to the _.map(list, iterator, [context]) Alias: collect Produces a new array of values by mapping each value in list through a transformation function ( _.reduce(list, iterator, memo, [context]) Aliases: inject, foldl Also known as inject and foldl, reduce boils down a list of values into a single value. _.reduceRight(list, iterator, memo, [context]) Alias: foldr The right-associative version of reduce. Delegates to the JavaScript 1.8 version of _.find(list, iterator, [context]) Alias: detect Looks through each value in the list, returning the first one that passes a truth test (iterator). The function returns as _.filter(list, iterator, [context]) Alias: select Looks through each value in the list, returning an array of all the values that pass a truth test ( _.where(list, properties) Looks through each value in the list, returning an array of all the values that contain all of the key-value pairs listed in _.findWhere(list, properties) Looks through the list and returns the first value that matches all of the key-value pairs listed in properties. _.reject(list, iterator, [context]) Returns the values in list without the elements that the truth test (iterator) passes. The opposite of filter. _.every(list, iterator, [context]) Alias: all Returns true if all of the values in the list pass the iterator truth test. Delegates to the native method _.some(list, [iterator], [context]) Alias: any Returns true if any of the values in the list pass the iterator truth test. Short-circuits and stops traversing the list _.contains(list, value) Alias: include Returns true if the value is present in the list. Uses indexOf internally, if list is an Array. _.invoke(list, methodName, [*arguments]) Calls the method named by methodName on each value in the list. Any extra arguments passed to _.pluck(list, propertyName) A convenient version of what is perhaps the most common use-case for map: extracting a list of property values. _.max(list, [iterator], [context]) Returns the maximum value in list. If iterator is passed, it will be used on each value to generate the criterion by which the _.min(list, [iterator], [context]) Returns the minimum value in list. If iterator is passed, it will be used on each value to generate the criterion by which the _.sortBy(list, iterator, [context]) Returns a sorted copy of list, ranked in ascending order by the results of running each value through iterator _.groupBy(list, iterator, [context]) Splits a collection into sets, grouped by the result of running each value through iterator. If iterator is a string instead of _.countBy(list, iterator, [context]) Sorts a list into groups and returns a count for the number of objects in each group. Similar to groupBy _.shuffle(list) Returns a shuffled copy of the list, using a version of the Fisher-Yates shuffle. _.toArray(list) Converts the list (anything that can be iterated over), into a real Array. Useful for transmuting the arguments object. _.size(list) Return the number of values in the list.
### 回答1: lodash.js是一个流行的JavaScript工具库,提供了许多实用的函数和方法,用于简化JavaScript编程的复杂性。如果你想要下载lodash.js,你可以采取以下几种方式: 1. 官方网站:你可以直接访问lodash.js的官方网站(https://lodash.com/)下载最新版本的lodash.js。在官方网站上,你可以找到针对不同需求的不同构建版本,包括完整版、核心版和按需版本。你只需点击相应的下载链接,即可将lodash.js下载到本地。 2. npm包管理工具:作为一个流行的npm包,你可以使用npm命令来下载lodash.js。首先,在你的项目文件夹中打开终端或命令提示符窗口,运行以下命令安装lodash.js: ``` npm install lodash ``` 这将自动下载并安装最新版本的lodash.js到你的项目中。 3. CDN:如果你只想暂时使用lodash.js而不想下载到本地,你可以使用CDN(内容分发网络)来引用lodash.js。一些常见的CDN提供商,如cdnjs、unpkg等,都提供了lodash.js的链接。你可以将这些链接直接复制到你的HTML文件中的`<script>`标签中,从而引入lodash.js。 无论你选择哪种方式,一旦你成功下载了lodash.js,你就可以在你的项目中引用它,并使用其中的函数和方法来简化你的JavaScript编程工作。 ### 回答2: lodash.js 是一个 JavaScript 库,提供了一组实用的函数,旨在简化处理数组、对象、字符串、函数等常见数据类型的操作。 要下载 lodash.js,可以按照以下步骤进行: 1. 打开 lodash.js 的官方网站(https://lodash.com/)。 2. 在页面中找到并点击 "Download"(下载) 按钮。 3. 在下载页面中,选择适合的版本和格式。 4. 如果你使用 npm(Node.js 包管理器),可以通过在终端或命令提示符中运行以下命令来安装 lodash: ``` npm install lodash ``` 5. 如果你在浏览器中使用 lodash,可以直接下载 lodash.js 文件,并将其引入你的网页中。点击下载链接,保存 lodash.js 文件到你的项目目录。 6. 在你的 HTML 文件中,使用 `<script>` 标签将 lodash.js 引入: ```html <script src="path/to/lodash.js"></script> ``` 其中 `path/to/lodash.js` 是你刚才下载 lodash.js 文件的路径。 现在,你就可以在你的项目中使用 lodash.js 提供的函数了。引入了 lodash.js 之后,可以通过 `_.`(underscorelodash.js 的函数别名)来调用库中的函数。 例如,你可以使用 `_.forEach` 函数来遍历一个数组: ```javascript _.forEach([1, 2, 3], function(value) { console.log(value); }); ``` 总之,下载 lodash.js 主要有两种方式:通过 npm 安装到你的项目中,或者直接下载 lodash.js 文件并在你的网页中引入。然后,你就可以愉快地使用 lodash.js 提供的实用函数来简化你的 JavaScript 编程。 ### 回答3: lodash.js是一个JavaScript的实用工具库,提供了许多常用的功能函数,可以简化开发过程中的操作。要下载lodash.js,可以采取以下步骤: 第一步,打开lodash.js的官方网站。可以通过搜索引擎搜索"lodash.js",找到官方网站的链接。 第二步,进入官方网站后,可以在网站上找到下载选项。一般来说,会有多个版本可供选择,包括编译后的版本和源代码版本。 第三步,选择需要下载的版本。编译后的版本适合直接在浏览器端使用,而源代码版本则可以进行自定义修改。根据自己的需求选择相应的版本。 第四步,完成版本选择后,点击下载链接。根据网速的不同,下载时间会有所不同。 第五步,下载完成后,将lodash.js文件保存到项目目录中的合适位置。可以根据自己的项目需求来确定所保存的路径。 第六步,将lodash.js引入到项目中的HTML文件中。可以使用<script>标签来引入,通过指定文件路径来将lodash.js加载到HTML中。 最后,将lodash.js成功下载并引入到项目中后,就可以在JavaScript代码中使用lodash.js提供的各种实用功能函数了,从而简化开发过程,提高开发效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值