[JS]递归对象或数组

function recursive(obj) {
    var output = '';
    if (typeof obj === 'object') {
        for (var key in obj) {
            var item = obj[key];
            if (typeof item === 'object') {
                output += recursive(item);
            } else {
                output += key + ':' + item + '\n';
            }
        }
    } else {
        output += obj + '\n';
    }
    return output;
}

 

posted @ 2016-12-13 15:32  炎峰森林影  阅读(4692)  评论(0编辑  收藏  举报