{"id":251339,"date":"2022-09-07T09:01:43","date_gmt":"2022-09-07T01:01:43","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=251339"},"modified":"2022-08-29T09:02:41","modified_gmt":"2022-08-29T01:02:41","slug":"lodash-tolength-topairs","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/lodash-tolength-topairs.html","title":{"rendered":"\u7b80\u5355\u4ecb\u7ecdlodash\u91cc\u7684toLength\u548ctoPairs\u65b9\u6cd5"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4e3a\u5927\u5bb6\u4ecb\u7ecd\u4e86lodash\u91cc\u7684toLength\u548ctoPairs\u65b9\u6cd5\u8be6\u89e3\uff0c\u6709\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u501f\u9274\u53c2\u8003\u4e0b\uff0c\u5e0c\u671b\u80fd\u591f\u6709\u6240\u5e2e\u52a9\uff0c\u795d\u5927\u5bb6\u591a\u591a\u8fdb\u6b65\uff0c\u65e9\u65e5\u5347\u804c\u52a0\u85aa<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u6b63\u6587<\/strong><\/div>\n

\u672c\u7bc7\u7ae0\u6211\u4eec\u5c06\u8ba4\u8bc6lodash\u91cc\u7684toLength\u65b9\u6cd5\u548ctoPairs\u65b9\u6cd5\u5b9e\u73b0\uff0c\u540c\u65f6\u5728\u5b9e\u73b0toPairs\u65b9\u6cd5\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u80fd\u4e86\u89e3\u5230\u5176\u4ed6\u5c01\u88c5\u7684\u5185\u90e8\u65b9\u6cd5\u7684\u5b9e\u73b0\u3002<\/p>\n

toLength<\/strong><\/div>\n

toLength\u65b9\u6cd5\u4e3b\u8981\u662f\u5c06\u53c2\u6570value\u8f6c\u6362\u4e3a\u7528\u4f5c\u7c7b\u6570\u7ec4\u5bf9\u8c61\u7684\u957f\u5ea6\u6574\u6570\u3002<\/p>\n

\u4f7f\u7528\u5982\u4e0b\uff1a<\/p>\n

toLength(3.2)\r\n\/\/ => 3\r\ntoLength(Number.MIN_VALUE)\r\n\/\/ => 0\r\ntoLength(Infinity)\r\n\/\/ => 4294967295\r\ntoLength('3.2')\r\n\/\/ => 3<\/pre>\n

\u5728\u5b9e\u73b0\u4e0a\uff0ctoLength\u65b9\u6cd5\u501f\u52a9\u5185\u90e8\u5c01\u88c5\u5bfc\u51fa\u7684toInteger\u65b9\u6cd5\uff0c\u5728 \u300a lodash\u91ccto\u7cfb\u5217\u4e4b\u5982\u4f55\u5c06\u6570\u636e\u8f6c\u6362\u6210\u6570\u5b57\u7c7b\u578b \u300b\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u4e86\u89e3\u4e86toInteger\u65b9\u6cd5\uff0c\u610f\u5728\u5c06\u53c2\u6570\u8f6c\u6362\u4e3a\u6574\u6570\u3002<\/p>\n

\u5b9e\u73b0\u4e0a\u5bf9\u4e8e\u4e0d\u5b58\u5728\u7684\u53c2\u6570\u76f4\u63a5\u8fd4\u56de0\uff0c\u5176\u6b21\u5c06\u53c2\u6570\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u5c0f\u4e8e0\u7684\u8fd4\u56de0\uff0c\u5927\u4e8e\u6700\u5927\u6570MAX_ARRAY_LENGTH\u8fd4\u56de\u6700\u5927\u6570\uff0c\u8be5\u65b9\u6cd5\u8fd4\u56de\u7684\u6574\u6570\u8303\u56f4\u4e3a[0,4294967295]\u3002<\/p>\n

\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n

import toInteger from '.\/toInteger.js'\r\nconst MAX_ARRAY_LENGTH = 4294967295\r\nfunction toLength(value) {\r\n  if (!value) {\r\n    return 0\r\n  }\r\n  value = toInteger(value)\r\n  if (value < 0) {\r\n    return 0\r\n  }\r\n  if (value > MAX_ARRAY_LENGTH) {\r\n    return MAX_ARRAY_LENGTH\r\n  }\r\n  return value\r\n}<\/pre>\n
toPairs<\/strong><\/div>\n

toPairs\u65b9\u6cd5\u4e3b\u8981\u662f\u521b\u5efa\u4e00\u4e2aobject\u5bf9\u8c61\u81ea\u8eab\u53ef\u679a\u4e3e\u5c5e\u6027\u7684\u952e\u503c\u5bf9\u6570\u7ec4\u3002\u8fd9\u4e2a\u6570\u7ec4\u53ef\u4ee5\u901a\u8fc7_.fromPairs\u64a4\u56de\u3002\u5982\u679cobject \u662f map \u6216 set\uff0c\u5c06\u4f1a\u8fd4\u56de\u5176\u6761\u76ee\u3002<\/p>\n

\u4f7f\u7528\u5982\u4e0b\uff1a<\/p>\n

function Foo() {\r\n this.a = 1;\r\n this.b = 2;\r\n}\r\nFoo.prototype.c = 3;\r\n_.toPairs(new Foo);\r\n\/\/ => [['a', 1], ['b', 2]]<\/pre>\n

toPairs\u65b9\u6cd5\u5728\u5b9e\u73b0\u4e0a\u501f\u52a9\u5185\u90e8\u5c01\u88c5\u7684createToPairs\u5de5\u5382\u51fd\u6570\u548ckeys\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5176\u4e2dkeys\u662f\u5bf9\u5916\u5bfc\u51fa\u7684\u65b9\u6cd5\u3002<\/p>\n

\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n

import createToPairs from '.\/_createToPairs.js';\r\nimport keys from '.\/keys.js';\r\nvar toPairs = createToPairs(keys);<\/pre>\n
createToPairs<\/strong><\/div>\n

createToPairs\u65b9\u6cd5\u662f\u5185\u90e8\u65b9\u6cd5\uff0c\u5145\u5f53\u5bf9\u8c61pairs\u8fc7\u7a0b\u7684\u5de5\u5382\u51fd\u6570\uff0c\u5b9e\u73b0\u4e0a\uff0c\u5148\u901a\u8fc7getTag\u83b7\u53d6\u5bf9\u8c61\u7684\u6570\u636e\u7c7b\u578b\u6807\u7b7e\uff0c\u5bf9\u4e8eMap\u5bf9\u8c61\u8fd4\u56demapToArray\u8c03\u7528\u7ed3\u679c\uff0c\u5bf9\u4e8eSet\u5bf9\u8c61\u8fd4\u56desetToPairs\u8c03\u7528\u7ed3\u679c\uff0c\u5176\u4ed6\u5bf9\u8c61\u7c7b\u578b\u76f4\u63a5\u8fd4\u56debaseToPairs\u8c03\u7528\u7ed3\u679c\u3002<\/p>\n

import baseToPairs from '.\/_baseToPairs.js';\r\nimport getTag from '.\/_getTag.js';\r\nimport mapToArray from '.\/_mapToArray.js';\r\nimport setToPairs from '.\/_setToPairs.js';\r\nvar mapTag = '[object Map]',\r\n    setTag = '[object Set]';\r\nfunction createToPairs(keysFunc) {\r\n  return function(object) {\r\n    var tag = getTag(object);\r\n    if (tag == mapTag) {\r\n      return mapToArray(object);\r\n    }\r\n    if (tag == setTag) {\r\n      return setToPairs(object);\r\n    }\r\n    return baseToPairs(object, keysFunc(object));\r\n  };\r\n}<\/pre>\n
baseToParis<\/strong><\/div>\n

baseToParis\u4f5c\u4e3a\u5185\u90e8\u65b9\u6cd5\uff0c\u4e3b\u8981\u5904\u7406\u666e\u901a\u5bf9\u8c61\u7684pairs\u8fc7\u7a0b\uff0c\u901a\u8fc7arrayMap\u65b9\u6cd5\u5904\u7406\u53c2\u6570\u3002<\/p>\n

import arrayMap from '.\/_arrayMap.js';\r\nfunction baseToPairs(object, props) {\r\n  return arrayMap(props, function(key) {\r\n    return [key, object[key]];\r\n  });\r\n}<\/pre>\n
arrayMap<\/strong><\/div>\n

arrayMap\u4f5c\u4e3a\u5185\u90e8\u65b9\u6cd5\uff0c\u4e3b\u8981\u5904\u7406\u6570\u7ec4\u7684\u6620\u5c04\u3002<\/p>\n

function arrayMap(array, iteratee) {\r\n  var index = -1,\r\n      length = array == null ? 0 : array.length,\r\n      result = Array(length);\r\n  while (++index < length) {\r\n    result[index] = iteratee(array[index], index, array);\r\n  }\r\n  return result;\r\n}<\/pre>\n
mapToArray<\/strong><\/div>\n

mapToArray\u4f5c\u4e3a\u5185\u90e8\u65b9\u6cd5\uff0c\u4e3b\u8981\u5904\u7406Map\u5bf9\u8c61\u8f6c\u6362\u6570\u7ec4\uff0c\u5b9e\u73b0\u4e0a\u901a\u8fc7\u904d\u5386\u83b7\u53d6\u3002<\/p>\n

function mapToArray(map) {\r\n  var index = -1,\r\n      result = Array(map.size);\r\n  map.forEach(function(value, key) {\r\n    result[++index] = [key, value];\r\n  });\r\n  return result;\r\n}<\/pre>\n
setToPairs<\/strong><\/div>\n

setToPairs\u4f5c\u4e3a\u5185\u90e8\u65b9\u6cd5\uff0c\u4e3b\u8981\u5904\u7406Set\u5bf9\u8c61\u8f6c\u6362\u6570\u7ec4\uff0c\u5b9e\u73b0\u4e0a\u901a\u8fc7\u904d\u5386\u83b7\u53d6\u3002<\/p>\n

function setToPairs(set) {\r\n  var index = -1,\r\n      result = Array(set.size);\r\n  set.forEach(function(value) {\r\n    result[++index] = [value, value];\r\n  });\r\n  return result;\r\n}<\/pre>\n
\u5c0f\u7ed3<\/strong><\/div>\n

\u672c\u7bc7\u7ae0\u6211\u4eec\u901a\u8fc7\u4e86\u89e3toLength\u65b9\u6cd5\u548ctoPairs\u65b9\u6cd5\u7684\u5b9e\u73b0\uff0c\u540c\u65f6\u4e5f\u8ba4\u8bc6\u4e86\u5982\u5904\u7406map\u548cset\u5411\u6570\u7ec4\u7684\u8f6c\u5316\uff0c\u4ee5\u53ca\u666e\u901a\u5bf9\u8c61\u8f6c\u6362\u6570\u7ec4\u7684baseToPairs\u65b9\u6cd5<\/p>\n","protected":false},"excerpt":{"rendered":"

\u672c\u7bc7\u7ae0\u6211\u4eec\u5c06\u8ba4\u8bc6lodash\u91cc\u7684toLength\u65b9\u6cd5\u548ctoPairs\u65b9\u6cd5\u5b9e\u73b0\uff0c\u540c\u65f6\u5728\u5b9e\u73b0toPairs\u65b9\u6cd5\u7684\u8fc7 […]<\/p>\n","protected":false},"author":1479,"featured_media":247462,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-251339","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/251339","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1479"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=251339"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/251339\/revisions"}],"predecessor-version":[{"id":251342,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/251339\/revisions\/251342"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/247462"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=251339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=251339"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=251339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}