实现 flatten 扁平化对象和数组

/**
 * 对象扁平化
 * 说明:请实现 flatten(input) 函数,input 为一个 javascript 对象(Object 或者 Array),返回值为扁平化后的结果。
 
 *   var input = {
 *     a: 1,
 *     b: [ 1, 2, { c: true }, [ 3 ] ],
 *     d: { e: 2, f: 3 },
 *     g: null, 
 *   }
 
 *   output如下
 *   {
 *     "a": 1,
 *     "b[0]": 1,
 *     "b[1]": 2,
 *     "b[2].c": true,
 *     "b[3][0]": 3,
 *     "d.e": 2,
 *     "d.f": 3,
 *     // "g": null,  值为null或者undefined,丢弃
 *  }
 */
 

<!DOCTYPE html>
<html>

<head>
    <title>Example Domain</title>

    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>

    <script>
        let result = {};
        const flatten = (params, key) => {
            if (params instanceof Array) {
                params.forEach((param, index) => {
                    if (param instanceof Object || param instanceof Array) {
                        flatten(param, `${key}[${index}]`);
                    } else {
                        result[`${key}[${index}]`] = flatten(param, `${key}[${index}]`);
                    }
                });
            } else if (params instanceof Object) {
                for (var itemKey in params) {
                    const itemValue = params[itemKey];
                    if (itemValue instanceof Array) {
                        flatten(itemValue, itemKey);
                    } else if (itemValue instanceof Object) {
                        flatten(itemValue, itemKey);
                    } else if (itemValue === null || itemValue === undefined) {} else {
                        if (key) {
                            result[`${key}.${itemKey}`] = flatten(itemValue, itemKey);
                        } else {
                            result[itemKey] = flatten(itemValue, itemKey);
                        }
                    }
                }
            } else {
                return params;
            }
        };

        var input = {
            a: 1,
            b: [
                1,
                2, {
                    c: true
                },
                [3]
            ],
            d: {
                e: 2,
                f: 3
            },
            g: null
        };
        flatten(input);
        console.log(result);
    </script>
</head>

<body>
    <div>
        <h1>Example Domain</h1>
        <p>
        <br>* 对象扁平化
        <br>* 说明:请实现 flatten(input) 函数,input 为一个 javascript 对象(Object 或者 Array),返回值为扁平化后的结果。
        <br>* 示例:
        <br>*   var input = {
        <br>*     a: 1,
        <br>*     b: [ 1, 2, { c: true }, [ 3 ] ],
        <br>*     d: { e: 2, f: 3 },
        <br>*     g: null, 
        <br>*   }
        <br>*   var output = flatten(input);
        <br>*   output如下
        <br>*   {
        <br>*     "a": 1,
        <br>*     "b[0]": 1,
        <br>*     "b[1]": 2,
        <br>*     "b[2].c": true,
        <br>*     "b[3][0]": 3,
        <br>*     "d.e": 2,
        <br>*     "d.f": 3,
        <br>*     // "g": null,  值为null或者undefined,丢弃
        <br>*  }
        <br>*/
        <br>function flatten() {
        <br> /* 代码实现 */
        <br>}
        </p>
      
    </div>
</body>

</html>

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值