UNPKG

jslib-tools

Version:

js工具库 封装常用的工具函数 如深拷贝 时间转换日期格式化、浏览器判断等,提高开发效率

206 lines (165 loc) 8.35 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: transfer/array.js</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="page-title">Source: transfer/array.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>/* * @Author: zhangyu * @Email: zhangdulin@outlook.com * @Date: 2021-07-08 09:50:14 * @LastEditors: zhangyu * @LastEditTime: 2021-07-28 14:18:10 * @Description: */ import { getType } from '../judgement/index' var slice = Array.prototype.slice; /** * 转为数组 * @param {Array|NodeList} obj 数组或类数组 * @param {number} from 开始索引,默认为0 */ export function toArray(obj, from) { var tmp = []; for (var i = from || 0, len = obj.length; i &lt; len; i++) { tmp.push(obj[i]); } return tmp; } //将 NodeList 转为 Array var makeArrayNode = (function () { try { slice.call(document.documentElement.childNodes); return function (obj, from) { return slice.call(obj, from); } } catch (e) { return toArray; } })(); /** * 将类数组对象转为数组,若对象不存在,则返回空数组 * @param {Array|arguments|NodeList} obj 数组或类数组 * @param {number} from 开始索引,默认为0 */ export function makeArray(obj, from) { if (obj == undefined) return []; switch (getType(obj)) { case "array": return from ? obj.slice(from) : obj; case "list": return makeArrayNode(obj, from); case "arguments": return slice.call(obj, from); } return [obj]; } // var arr = [ // {id: 1, name: '部门1', pid: 0}, // {id: 2, name: '部门2', pid: 1}, // {id: 3, name: '部门3', pid: 1}, // {id: 4, name: '部门4', pid: 3}, // {id: 5, name: '部门5', pid: 4}, // ] /** * @description: 扁平=>tree 递归查找,获取children * @param {*} data 要处理对象 * @param {*} result 返回数组 * @param {*} pid 父id * @return {array} */ var getChildren = function (data, result, pid) { for (const item of data) { if (item.pid === pid) { const newItem = { ...item, children: [] }; result.push(newItem); getChildren(data, newItem.children, item.id); } } } /** * 转换方法 */ const arrayToTree = function (data, pid) { const result = []; getChildren(data, result, pid) return result; } function arrayToTree2(items) { const result = []; // 存放结果集 const itemMap = {}; // for (const item of items) { const id = item.id; const pid = item.pid; if (!itemMap[id]) { itemMap[id] = { children: [], } } itemMap[id] = { ...item, children: itemMap[id]['children'] } const treeItem = itemMap[id]; if (pid === 0) { result.push(treeItem); } else { if (!itemMap[pid]) { itemMap[pid] = { children: [], } } itemMap[pid].children.push(treeItem) } } return result; } function arrayToTree3(data) { const link = new Map(); function getChild(pid) { if (!link.get(pid)) link.set(pid, []); return link.get(pid); } for (let i = 0; i &lt; data.length; i++) { const one = data[i]; getChild(one.pid).push({ ...one, children: getChild(one.id) }); } return link.get(0); } function arrayToTree4(data, pid) { let newArray = [] data.forEach(iterator => { if (iterator.pid == pid) { if (iterator.children) { iterator.children.push(arrayToTree4(data, iterator.id)) } else { iterator.children = arrayToTree4(data, iterator.id) } newArray.push(iterator) } }); return newArray }</code></pre> </article> </section> </div> <nav> <h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#alias">alias</a></li><li><a href="global.html#arrayMove">arrayMove</a></li><li><a href="global.html#arrayMoveMutate">arrayMoveMutate</a></li><li><a href="global.html#arrayToObject">arrayToObject</a></li><li><a href="global.html#arrayToTree">arrayToTree</a></li><li><a href="global.html#arrFibonacci">arrFibonacci</a></li><li><a href="global.html#arrVals">arrVals</a></li><li><a href="global.html#callHandler">callHandler</a></li><li><a href="global.html#camelize">camelize</a></li><li><a href="global.html#checkInt">checkInt</a></li><li><a href="global.html#checkNum">checkNum</a></li><li><a href="global.html#clipboardObj">clipboardObj</a></li><li><a href="global.html#compareSize">compareSize</a></li><li><a href="global.html#cookie">cookie</a></li><li><a href="global.html#cursortPosition">cursortPosition</a></li><li><a href="global.html#dasherize">dasherize</a></li><li><a href="global.html#dateFormat1">dateFormat1</a></li><li><a href="global.html#dateFormat2">dateFormat2</a></li><li><a href="global.html#debounce">debounce</a></li><li><a href="global.html#decode_url_param">decode_url_param</a></li><li><a href="global.html#def">def</a></li><li><a href="global.html#elDateFormat">elDateFormat</a></li><li><a href="global.html#escapeHTML">escapeHTML</a></li><li><a href="global.html#exportXls">exportXls</a></li><li><a href="global.html#extend">extend</a></li><li><a href="global.html#getBrowserModel">getBrowserModel</a></li><li><a href="global.html#getChangedData">getChangedData</a></li><li><a href="global.html#getChildren">getChildren</a></li><li><a href="global.html#getCookie">getCookie</a></li><li><a href="global.html#getDefaultAvatar">getDefaultAvatar</a></li><li><a href="global.html#getDeviceModel">getDeviceModel</a></li><li><a href="global.html#getHttpBase64">getHttpBase64</a></li><li><a href="global.html#getImgBase64">getImgBase64</a></li><li><a href="global.html#getThumbnails">getThumbnails</a></li><li><a href="global.html#getType">getType</a></li><li><a href="global.html#handleEmoji">handleEmoji</a></li><li><a href="global.html#handleParam">handleParam</a></li><li><a href="global.html#handleText">handleText</a></li><li><a href="global.html#isArray">isArray</a></li><li><a href="global.html#isArrayLike">isArrayLike</a></li><li><a href="global.html#isDiff">isDiff</a></li><li><a href="global.html#isFunc">isFunc</a></li><li><a href="global.html#isInt">isInt</a></li><li><a href="global.html#isNum">isNum</a></li><li><a href="global.html#isObject">isObject</a></li><li><a href="global.html#isObjEmpty">isObjEmpty</a></li><li><a href="global.html#isUInt">isUInt</a></li><li><a href="global.html#isUNum">isUNum</a></li><li><a href="global.html#makeArray">makeArray</a></li><li><a href="global.html#objectConversionToList">objectConversionToList</a></li><li><a href="global.html#objectToArray">objectToArray</a></li><li><a href="global.html#objTools">objTools</a></li><li><a href="global.html#parseTime">parseTime</a></li><li><a href="global.html#setVideoPlay">setVideoPlay</a></li><li><a href="global.html#sleepAction">sleepAction</a></li><li><a href="global.html#sleepSync">sleepSync</a></li><li><a href="global.html#sortMapByKey">sortMapByKey</a></li><li><a href="global.html#storage">storage</a></li><li><a href="global.html#throttle">throttle</a></li><li><a href="global.html#titleize">titleize</a></li><li><a href="global.html#toArray">toArray</a></li><li><a href="global.html#toLower">toLower</a></li><li><a href="global.html#toMap">toMap</a></li><li><a href="global.html#toObjectMap">toObjectMap</a></li><li><a href="global.html#toSimplifiedChinese">toSimplifiedChinese</a></li><li><a href="global.html#toUpper">toUpper</a></li><li><a href="global.html#unescapeHTML">unescapeHTML</a></li></ul> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.7</a> on Wed Aug 25 2021 17:57:45 GMT+0800 (GMT+08:00) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>