官术网_书友最值得收藏!

The difference between arrays and collections

One of the initial sources of confusion for newcomers to Lo-Dash is the distinction between arrays and collections. The Lo-Dash API has a set of functions for arrays, and a set of functions for collections. But why? It would appear that these functions are interchangeable for any collection. Well, a better definition of what a collection actually is according to Lo-Dash might clear things up.

A collection is an abstract concept. That is, we can use the collection functions found in Lo-Dash on any JavaScript object that we'd like to iterate over. For example, the forEach() function will happily iterate over an array, a string, or an object. The subtle differences between these types, and what they mean when it comes to iterating, are hidden from the developer.

The array functions provided by Lo-Dash are less abstract, they do, in fact, expect an array. In a sense, even these functions are abstract because they don't explicitly check for the Array type. They require that the object supports numerical indices and that it has a numerical length property.

The takeaway is that, in the overwhelming majority of your days as a Lo-Dash programmer, the distinction between arrays and collections does not matter. Mainly, because the primary collection type will be an array anyway. In a small minority of cases, where the distinction does matter, just remember that the array functions have a slightly stricter criteria for what they consider acceptable data.

主站蜘蛛池模板: 陕西省| 阿图什市| 临夏县| 革吉县| 高唐县| 海丰县| 三穗县| 尼玛县| 普定县| 南充市| 广水市| 江城| 正定县| 依安县| 保定市| 临潭县| 兰考县| 珲春市| 文化| 桂阳县| 奉贤区| 喀什市| 巴青县| 前郭尔| 沁阳市| 稻城县| 田东县| 武强县| 莱阳市| 富蕴县| 金华市| 万荣县| 获嘉县| 昌宁县| 闽侯县| 龙山县| 阜康市| 龙川县| 门源| 紫金县| 黄陵县|