zoukankan      html  css  js  c++  java
  • 将一个多维数组变为一个一维数组

     1 var arr=[1,2,3,[4,5],[6,[7,[8]]]]
     2 /**
     3  * 使用递归的方式处理
     4  * wrap内保存结果ret 
     5  * 返回一个递归函数
     6  *
     7  * @returns
     8  */
     9 function wrap(){
    10     var ret=[];
    11     return function flat(a){
    12         for(var item of a){
    13             if(item.constructor===Array){
    14                 ret.concat(flat(item))
    15             }else{
    16                 ret.push(item)
    17             }
    18         }
    19         return ret
    20     }
    21 }
    22 console.log(wrap()(arr));
     1 const spreadableSymbol = Symbol.isConcatSpreadable
     2 const isFlattenable = (value) => {
     3   return Array.isArray(value) || (typeof value == 'object' && value !== null
     4     && Object.prototype.toString.call(value) === '[object Arguments]') ||
     5     !!(spreadableSymbol && value && value[spreadableSymbol])
     6 }
     7 
     8 /**10  * @param array 需要展开的数组
    11  * @param depth 展开深度
    12  * @param predicate 迭代时需要调用的函数
    13  * @param isStrict 限制通过`predicate`函数检查的值
    14  * @param result 初始结果值
    15  * @returns {Array} 返回展开后的数组
    16  */
    17 function flatten(array, depth, predicate, isStrict, result) {
    18   predicate || (predicate = isFlattenable)
    19   result || (result = [])
    20 
    21   if (array == null) {
    22     return result
    23   }
    24 
    25   for (const value of array) {
    26     if (depth > 0 && predicate(value)) {
    27       if (depth > 1) {
    28         flatten(value, depth - 1, predicate, isStrict, result)
    29       } else {
    30         result.push(...value)
    31       }
    32     } else if (!isStrict) {
    33       result[result.length] = value
    34     }
    35   }
    36   return result
    37 }
    38 
    39 flatten([1, 2, 3, [4, 5, [6]]], 2)
     1 let arr = [1, 2, [3, 4, 5, [6, 7], 8], 9, 10, [11, [12, 13]]]
     2 
     3 const flatten = function (arr) {
     4     while (arr.some(item => Array.isArray(item))) {
     5         arr = [].concat(...arr)
     6     }
     7     return arr
     8 }
     9 
    10 console.log(flatten(arr))
    11 
    12 const flatten = array => array.reduce((acc, cur) => (Array.isArray(cur) ? [...acc, ...flatten(cur)] : [...acc, cur]), [])
     1 let arr = [1, 2, [3, 4, 5, [6, 7], 8], 9, 10, [11, [12, 13]]]
     2 /*
     3 *迭代实现
     4 */
     5     function flatten(arr) {
     6       let arrs =[...arr]
     7       let newArr = [];
     8       while (arrs.length){
     9         let item = arrs.shift()
    10         if(Array.isArray(item)){
    11           arrs.unshift(...item)
    12         }else {
    13           newArr.push(item)
    14         }
    15       }
    16       return newArr
    17     }
    18 /*
    19 *递归实现
    20 */
    21     function flatten(arr) {
    22     let arrs = [];
    23       arr.map(item => {
    24         if(Array.isArray(item)){
    25           arrs.push(... flatten(item))
    26         } else {
    27           arrs.push(item)
    28         }
    29       })
    30       return arrs
    31     }
    1 /*切割字符串*/
    2 arr.join(',').split(',').map(item => Number(item)))
     1 使用Generator实现数组flatten:
     2 
     3 function* flat(arr){
     4     for(let item of arr){
     5         if(Array.isArray(item)){
     6             yield* flat(item);//Generator委托
     7         }else {
     8             yield item
     9         }
    10     }
    11 }
    12 function flatten(arr) {
    13     let result = [];
    14     for(let val of(flat(arr))){
    15         result.push(val);
    16     }
    17     return result;
    18 }
    19 let arr1 = [1,[2,3,[4,5],6],[7]];
    20 console.log(flatten(arr1));//[1, 2, 3, 4, 5, 6, 7]
     1 const arr = [1,2,3,[4,5],6,[7,[8, 9, 10]]]
     2 
     3 // 递归
     4 function flatten(arr, res = []) {
     5     for (let i = 0; i < arr.length; i++) {
     6         if (typeof arr[i] === 'object') {
     7             flatten(arr[i], res);
     8         } else {
     9             res.push(arr[i]);
    10         }
    11     }
    12     return res;
    13 }
    14 
    15 // 队列
    16 function flatten1(arr) {
    17     const newArr = [...arr];
    18     const res = [];
    19     while (newArr.length) {
    20         const item = newArr.shift(); // 出队
    21         if (Array.isArray(item)) {
    22             newArr.unshift(...item); // 入队
    23         } else {
    24             res.push(item);
    25         }
    26     }
    27     return res;
    28 }
    1 /*@param {Array} arr 需要扁平化的数组
    2 @param {Number} [depth=1] 扁平化深度
    3 @return {Array} 扁平化后的新数组
    4 */
    5 function flatten(arr, depth = 1) {
    6 return arr.reduce((newArr, nextArr) => newArr.concat(depth > 1 && Array.isArray(nextArr) ? flatten(nextArr, depth - 1) : nextArr), [])
    7 }
     1 function flatten(arr) {
     2   const result = [...arr];
     3   for (let i = 0; i < result.length; i++) {
     4     const item = result[i]
     5     if (Array.isArray(item)) {
     6       Array.prototype.splice.apply(result, [i, 1, ...item])
     7       i--
     8     }
     9   }
    10   return result
    11 }
  • 相关阅读:
    Spring异常之版本错误
    SpringMVC格式转化错误之HTTP Status [400] – [Bad Request]
    Spring错误之org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'bookService' is expected to be of type 'pw.fengya.tx.BookService' but was actually of type 'com.sun.proxy.$Proxy1
    02_版本控制工具SVN
    Hibernate异常之命名查询节点未找到
    Hibernate异常之Integer转float(自动类型转换错误)
    Hibernate异常之cascade
    Hibernate异常之关键字错误
    DHCP保留地址批量导入导出
    H3C和CISCO交换机禁止MAC地址通信
  • 原文地址:https://www.cnblogs.com/MrHaoRoot/p/10562910.html
Copyright © 2011-2022 走看看