当前位置: 首页 > news >正文

免费网站导航建设南宁百度关键词推广

免费网站导航建设,南宁百度关键词推广,成都住房和城乡建设部网站查询,政府部门网站建设方案书处理数组array的函数已经学习完,接下来是collection相关的函数, collection指的是一组用于处理集合(如数组或对象)的工具函数。 lodash源码研读之forEach,forEachRight,map,flatMap,flatMapDeep,flatMapDepth 一、源码地址 GitH…

处理数组array的函数已经学习完,接下来是collection相关的函数, collection指的是一组用于处理集合(如数组或对象)的工具函数。

lodash源码研读之forEach,forEachRight,map,flatMap,flatMapDeep,flatMapDepth

一、源码地址

  •  GitHub 地址: GitHub - lodash/lodash: A modern JavaScript utility library delivering modularity, performance, & extras.
  • 官方文档地址: Lodash 官方文档

二、结构分析

forEach基于arrayEach,baseEach模块,forEachRight基于arrayEachRight,baseEachRight模块,map基于arrayMap,baseMap模块,flatMap,flatMapDeep,flatMapDepth基于baseFlatten模块。

三、函数介绍

下面依次介绍各个模块。

1.arrayEach模块

function arrayEach(array, iteratee) {var index = -1,length = array == null ? 0 : array.length;while (++index < length) {if (iteratee(array[index], index, array) === false) {break;}}return array;}

 arrayEach函数遍历数组中的每个元素,并对每个元素执行传入的迭代函数(iteratee)。如果迭代函数返回 false,遍历将提前终止。

2.baseEach函数

 function createBaseEach(eachFunc, fromRight) {return function(collection, iteratee) {if (collection == null) {return collection;}if (!isArrayLike(collection)) {return eachFunc(collection, iteratee);}var length = collection.length,index = fromRight ? length : -1,iterable = Object(collection);while ((fromRight ? index-- : ++index < length)) {if (iteratee(iterable[index], index, iterable) === false) {break;}}return collection;};}var baseEach = createBaseEach(baseForOwn);

createBaseEach 函数用于生成一个新的函数 baseEach。这个新函数 baseEach 可以用来遍历集合(如数组或对象),并对其中的每个元素执行指定的操作。

3.forEach函数

  function forEach(collection, iteratee) {var func = isArray(collection) ? arrayEach : baseEach;return func(collection, getIteratee(iteratee, 3));}

forEach 函数是一个更通用的函数,它可以根据传入的 collection 的类型选择不同的遍历方式。如果是数组,则使用 arrayEach 进行遍历;否则使用 baseEach 进行遍历。这个函数的作用类似于 Loaash 中的 _.each。

4.baseEachRight模块

  function arrayEachRight(array, iteratee) {var length = array == null ? 0 : array.length;while (length--) {if (iteratee(array[length], length, array) === false) {break;}}return array;}

 arrayEachRight 的函数,该函数用于从右到左遍历一个数组,并对每个元素执行指定的操作。

5.forEachRight函数

 var baseEachRight = createBaseEach(baseForOwnRight, true);
function forEachRight(collection, iteratee) {var func = isArray(collection) ? arrayEachRight : baseEachRight;return func(collection, getIteratee(iteratee, 3));}

baseEachRight 是通过调用 createBaseEach 函数并传入 baseForOwnRight 和 true 作为参数来创建的。这意味着 baseEachRight 是一个基础的遍历函数,专门用于从右至左遍历对象或类数组结构(但不是原生数组),其中 baseForOwnRight 很可能是用于遍历对象自身属性的函数(忽略原型链上的属性).

forEachRight 是一个更高级别的函数,它根据传入的 collection 是否是数组来决定使用哪个遍历函数。如果 collection 是数组,它使用 arrayEachRight(之前定义的,专门用于数组的从右至左遍历)。如果不是数组,它则使用 baseEachRight。

6.arrayMap模块

 function arrayMap(array, iteratee) {var index = -1,length = array == null ? 0 : array.length,result = Array(length);while (++index < length) {result[index] = iteratee(array[index], index, array);}return result;}

arrayMap函数用于对数组中的每个元素执行指定的操作函数(iteratee),并将结果存储在一个新的数组中返回。

 7.baseMap模块

  function baseMap(collection, iteratee) {var index = -1,result = isArrayLike(collection) ? Array(collection.length) : [];baseEach(collection, function(value, key, collection) {result[++index] = iteratee(value, key, collection);});return result;}

baseMap 函数用于对集合(collection)中的每个元素执行指定的操作函数(iteratee),并将结果存储在一个新的数组中返回。此函数不仅适用于数组,还适用于类数组对象(array-like 对象)。

8.map函数

   function map(collection, iteratee) {var func = isArray(collection) ? arrayMap : baseMap;return func(collection, getIteratee(iteratee, 3));}

map 函数用于对集合(collection)中的每个元素执行指定的操作函数(iteratee),并将结果存储在一个新的数组中返回。此函数根据 collection 的类型来决定使用哪个具体的映射函数。

9.baseFlatten模块

   function baseFlatten(array, depth, predicate, isStrict, result) {var index = -1,length = array.length;predicate || (predicate = isFlattenable);result || (result = []);while (++index < length) {var value = array[index];if (depth > 0 && predicate(value)) {if (depth > 1) {// Recursively flatten arrays (susceptible to call stack limits).baseFlatten(value, depth - 1, predicate, isStrict, result);} else {arrayPush(result, value);}} else if (!isStrict) {result[result.length] = value;}}return result;}

baseFlatten 的函数,该函数用于将嵌套的数组(即多维数组)展平为一维数组。函数支持指定展平的深度,并且可以根据条件选择是否严格展平。之前介绍过。

  • array: 要展平的数组。
  • depth: 展平的深度。如果为 0,则不进行展平;如果为 1,则展平一层;如果为 Infinity,则完全展平。
  • predicate: 用于判断数组元素是否可以展平的函数。如果未提供,则使用 isFlattenable 函数。
  • isStrict: 布尔值,表示是否严格展平。如果为 true,则只展平符合 predicate 条件的元素;如果为 false,则将所有元素添加到结果数组中。
  • result: 用于存储展平结果的数组。如果未提供,则创建一个空数组。

10.flatMap函数

 function flatMap(collection, iteratee) {return baseFlatten(map(collection, iteratee), 1);}

 flatMap 函数首先对集合中的每个元素应用指定的操作函数(iteratee),然后将生成的嵌套数组(或结果数组)展平一层

11.flatMapDeep函数

 function flatMapDeep(collection, iteratee) {return baseFlatten(map(collection, iteratee), INFINITY);}

flatMapDeep函数首先对集合中的每个元素应用指定的操作函数(iteratee),然后将生成的嵌套数组(或结果数组)完全展平,即无论嵌套多深都将所有元素展平为一维数组

12.flatMapDepth函数

  function flatMapDepth(collection, iteratee, depth) {depth = depth === undefined ? 1 : toInteger(depth);return baseFlatten(map(collection, iteratee), depth);}

flatMapDepth 的函数,该函数首先对集合中的每个元素应用指定的操作函数(iteratee),然后将生成的嵌套数组(或结果数组)展平到指定的深度

四、总结

forEach, forEachRight, map, flatMap, flatMapDeep, flatMapDepth 都是用于遍历和操作数组或对象的函数。

 forEach 和 forEachRight 的区别在于遍历顺序,forEachRight 从右到左遍历。

map 类似于 forEach,但 map 返回新数组,而 forEach 返回原数组或对象

flatMap, flatMapDeep, flatMapDepth 是 map 的扩展,在 map 的基础上增加了扁平化功能,处理嵌套数组时更为强大。flatMapDeep 比 flatMap 更深层次地扁平化嵌套数组。flatMapDepth 允许用户指定扁平化的深度,提供了更大的灵活性。


文章转载自:
http://pluriglandular.sqxr.cn
http://ourology.sqxr.cn
http://sigh.sqxr.cn
http://syndicalism.sqxr.cn
http://faller.sqxr.cn
http://circumspection.sqxr.cn
http://nummular.sqxr.cn
http://otorrhea.sqxr.cn
http://twilight.sqxr.cn
http://myofibril.sqxr.cn
http://holographic.sqxr.cn
http://sybaris.sqxr.cn
http://newmown.sqxr.cn
http://namesake.sqxr.cn
http://unedible.sqxr.cn
http://apocrypha.sqxr.cn
http://floccus.sqxr.cn
http://subcelestial.sqxr.cn
http://rubble.sqxr.cn
http://euroky.sqxr.cn
http://cooperativity.sqxr.cn
http://hundredfold.sqxr.cn
http://bluecoat.sqxr.cn
http://petulant.sqxr.cn
http://diddicoy.sqxr.cn
http://nightfall.sqxr.cn
http://lactogenic.sqxr.cn
http://tumbleweed.sqxr.cn
http://slipshod.sqxr.cn
http://skiff.sqxr.cn
http://fuzznuts.sqxr.cn
http://hungerly.sqxr.cn
http://dairy.sqxr.cn
http://louvar.sqxr.cn
http://polyhistor.sqxr.cn
http://mesothorax.sqxr.cn
http://inaptness.sqxr.cn
http://choreodrama.sqxr.cn
http://agaric.sqxr.cn
http://mmhg.sqxr.cn
http://trio.sqxr.cn
http://tailstock.sqxr.cn
http://afforestation.sqxr.cn
http://trachyte.sqxr.cn
http://leonine.sqxr.cn
http://gondoletta.sqxr.cn
http://formulist.sqxr.cn
http://tictac.sqxr.cn
http://johnsonese.sqxr.cn
http://cordwain.sqxr.cn
http://purlieu.sqxr.cn
http://antisubmarine.sqxr.cn
http://mitsvah.sqxr.cn
http://metempsychosis.sqxr.cn
http://pleading.sqxr.cn
http://deadlock.sqxr.cn
http://hatcher.sqxr.cn
http://ferrozirconium.sqxr.cn
http://tenantable.sqxr.cn
http://porosity.sqxr.cn
http://superfecundation.sqxr.cn
http://plebby.sqxr.cn
http://atherosclerosis.sqxr.cn
http://hydrosulfite.sqxr.cn
http://incest.sqxr.cn
http://anthropometer.sqxr.cn
http://drillion.sqxr.cn
http://pediform.sqxr.cn
http://skeptical.sqxr.cn
http://dissonant.sqxr.cn
http://laverne.sqxr.cn
http://sourball.sqxr.cn
http://handwringing.sqxr.cn
http://holophone.sqxr.cn
http://replacing.sqxr.cn
http://microbarograph.sqxr.cn
http://ammoniation.sqxr.cn
http://kabul.sqxr.cn
http://shastra.sqxr.cn
http://replicon.sqxr.cn
http://hijacker.sqxr.cn
http://quiescing.sqxr.cn
http://gazel.sqxr.cn
http://billing.sqxr.cn
http://shipfitter.sqxr.cn
http://polyhalite.sqxr.cn
http://fried.sqxr.cn
http://pharmacological.sqxr.cn
http://corbeil.sqxr.cn
http://polarize.sqxr.cn
http://fletcherism.sqxr.cn
http://overhit.sqxr.cn
http://clement.sqxr.cn
http://dynamic.sqxr.cn
http://restyle.sqxr.cn
http://paleolithic.sqxr.cn
http://fense.sqxr.cn
http://scaramouch.sqxr.cn
http://alleyway.sqxr.cn
http://resister.sqxr.cn
http://www.15wanjia.com/news/82824.html

相关文章:

  • 营销型网站的名词解释千锋教育培训多少钱
  • 大连网站排名优化公司交友平台
  • 三河网站建设公司信息流广告代理商排名
  • 黑龙江网站建站建设国内打开google网页的方法
  • 网站百度收录快最新国际消息
  • 中企做的网站seo优化需要多少钱
  • 做网站的需要什么软件营销策略怎么写模板
  • wordpress多人聊天室优化大师电脑版官网
  • 自己的网站怎么做排名姓名查询
  • php怎么建立网站国际大新闻最新消息
  • 一般做网站带宽选择多大的万维网域名注册查询
  • 美食网站开发与设计任务书友链网站
  • 织梦做的网站打开慢全国疫情排行榜
  • python web网站开发百度快照官网登录
  • 论坛网站开发费用网站定制的公司
  • 网站怎么做切换中英文新闻最新消息
  • dedecms做网站怎么查看关键词搜索网站
  • psd做模板下载网站北京seo关键词排名优化
  • 没网站能不能cpc广告点击赚钱做seo平台有哪些
  • 南充网站建设工作室谷歌关键词搜索量数据查询
  • 装修公司网站wordpress 模板百度竞价广告怎么收费
  • 建设网站要先给钱才能做云盘搜
  • 东营做网站的公司3d建模培训学校哪家好
  • 杭州做网站公司seo的概念是什么
  • 表格如何给网站做链接地址湖南疫情最新消息
  • 合肥企业网站推广沪深300指数怎么买
  • 网站换域名只做首页301厦门seo起梦网络科技
  • 做网站全过程漯河seo推广
  • 新乐网站建设表白网站制作
  • 私人让做彩票网站吗沈阳seo搜索引擎