递归减少嵌套对象未定义

时间:2018-10-09 06:36:58

标签: javascript

所以我正在尝试执行递归函数,但是我不确定为什么嵌套对象返回的是未定义的。

代码:

var cleanServerObject =
    function (object)
    {
        Object.keys(object).reduce(
            function (new_object, key)
            {
                let new_key = key.replace(/^__/, "")
                console.log("new_object", new_object)
                console.log("current key", key, "new_key", new_key)
                new_object[new_key] =
                    (typeof object[key] === "object" && !Array.isArray(object[key]))
                        ? cleanServerObject(object[key])
                        : object[key]
                return new_object
            },
            {}
        )
    }
    
var json = {"_id":"5bbc3e5962a7e0b912f54b48","__display_text":"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean m","__hidden":true,"__image":{"__style":"cover","__url":"http://via.placeholder.com/400x700/d3d3d3/000000/?text=No%20Image&"},"__price":{"__banner":"none","__current":0,"__default":0,"__extra_info":"","__ignore":true,"__measurement":""},"__tags":[],"__details":[],"__list_id":"5bb17bdec7fb946609ce8bd4","__created":"2018-10-09T05:36:25.353Z","__v":0}

cleanServerObject(json)

JSON:

{
    "_id": "5bbc3e5962a7e0b912f54b48",
    "__display_text": "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean m",
    "__hidden": true,
    "__image": {
        "__style": "cover",
        "__url": "http://via.placeholder.com/400x700/d3d3d3/000000/?text=No%20Image&"
    },
    "__price": {
        "__banner": "none",
        "__current": 0,
        "__default": 0,
        "__extra_info": "",
        "__ignore": true,
        "__measurement": ""
    },
    "__tags": [],
    "__details": [],
    "__list_id": "5bb17bdec7fb946609ce8bd4",
    "__created": "2018-10-09T05:36:25.353Z",
    "__v": 0
}

1 个答案:

答案 0 :(得分:4)

您自己的递归函数中缺少return语句。

尝试

var cleanServerObject = function (object) {
// -vvvvvv--- this thing!!
    return Object.keys(object).reduce(
// -^^^^^^--- yes, this one
        function (new_object, key)
        {
            let new_key = key.replace(/^__/, "")
            console.log("new_object", new_object)
            console.log("current key", key, "new_key", new_key)
            new_object[new_key] =
                (typeof object[key] === "object" && !Array.isArray(object[key]))
                    ? cleanServerObject(object[key])
                    : object[key]
            return new_object
        },
        {}
    )
}

var json = {"_id":"5bbc3e5962a7e0b912f54b48","__display_text":"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean m","__hidden":true,"__image":{"__style":"cover","__url":"http://via.placeholder.com/400x700/d3d3d3/000000/?text=No%20Image&"},"__price":{"__banner":"none","__current":0,"__default":0,"__extra_info":"","__ignore":true,"__measurement":""},"__tags":[],"__details":[],"__list_id":"5bb17bdec7fb946609ce8bd4","__created":"2018-10-09T05:36:25.353Z","__v":0}

cleanServerObject(json)