zoukankan      html  css  js  c++  java
  • JavaScript 数据扁平化处理

    // 数组扁平化处理
    [1,2,[3,4,[5,6]]].flat();
    
    // 对象扁平化处理
    Object.flatten = function(obj){
        var result = {};
    
        function recurse(src, prop) {
            var toString = Object.prototype.toString;
            if (toString.call(src) == '[object Object]') {
                var isEmpty = true;
                for (var p in src) {
                    isEmpty = false;
                    recurse(src[p], prop ? prop + '.' + p : p)
                }
                if (isEmpty && prop) {
                    result[prop] = {};
                }
            } else if (toString.call(src) == '[object Array]') {
                var len = src.length;
                if (len > 0) {
                    src.forEach(function (item, index) {
                        recurse(item, prop ? prop + '.[' + index + ']' : index);
                    })
                } else {
                    result[prop] = [];
                }
            } else {
                result[prop] = src;
            }
        }
        recurse(obj,'');
    
        return result;
    }
  • 相关阅读:
    VS快捷键
    IIS部署WCF
    WLAN的优点
    局域网与WAN比较
    局域网拓扑结构
    局域网协议
    局域网介绍
    wifi主要特性
    wifi发展前景
    Wi-Fi与WAPI主要区别
  • 原文地址:https://www.cnblogs.com/GongYaLei/p/11672013.html
Copyright © 2011-2022 走看看