从此递归循环返回累积的字符串

时间:2015-07-18 23:44:56

标签: javascript recursion

我正在尝试返回一个最终字符串,它是每个嵌套对象的字符串值的串联。我无法做对。

我想我知道发生了什么事;问题是每次调用return而不检查条件,因此当函数调用自身时,变量_text再次变为空。我应该检查return语句的条件。

input只是一个例子,数组包含任意数量的项目,每个项目可以是字符串,对象或数组。请帮忙。谢谢你的时间

var input = ["\n\t  This function constructs a named and protected temporal collection \n\t  in the schema database with the specified system and valid axes.  \n\t  This function assumes that the system and valid axes already exist.  \n\t  The temporal collection is stored in the Security database.  \n\t  ", {
    "p": {
        "_children": ["A TEMPORAL-DUPCOLLECTION exception is thrown if the collection \n          already exists.  "]
    }
}, {
    "p": {
        "_children": ["\n  For details on how to create a collection, see  \n ", {
            "a": {
                "_attributes": {
                    "href": "#display.xqy?fname=http://pubs/6.0doc/xml/temporal/temporal-quick-start.xml%2366392"
                },
                "_children": ["Create a Temporal Collection"]
            }
        }, " in the ", {
            "em": {
                "_children": ["Temporal Developer's Guide"]
            }
        }]
    }
}];



function cleanupString(str) {
    return str.replace(/(\n|\t|\s+)/g, " ").trim();
}

function parseSummary(text, item) {
   // console.log('RAW-----:' + JSON.stringify(item))

    var _text =  text || '';

    if (typeof item === 'string') {
        _text = _text + item;
        _text = cleanupString(_text);
    } 
        // if item is object
    else if (item.constructor == Object) {
        for (var key in item) {
           return parseSummary(_text, item[key]);
        }
    } // if item is array
    else if (item.constructor == Array) {
        item.map(function(k) {
            return parseSummary(_text, k);
        });
    } 


        console.log('___TEXT____', _text);                      
        // I think the because of this return being called prematurely, the 
        // variable _text is reset to empty, but not sure what condition I 
        // should check  because there is no way of knowing if its reached end 
        // of the object 
        return _text; 

}
var summary = parseSummary('', input);
console.log('SUMMARY:', summary); // comes out as empty

登录

 ___TEXT____ This function constructs a named and protected temporal collection in the schema database with the specified system and valid axes. This function assumes that the system and valid axes already exist. The temporal collection is stored in the Security database.
 ___TEXT____ A TEMPORAL-DUPCOLLECTION exception is thrown if the collection already exists.
 ___TEXT____ 
 ___TEXT____ For details on how to create a collection, see
 ___TEXT____ #display.xqy?fname=http://pubs/6.0doc/xml/temporal/temporal-quick-start.xml%2366392
 ___TEXT____ in the
 ___TEXT____ Temporal Developer's Guide
 ___TEXT____ 
 ___TEXT____ 
 ___TEXT____ 
 SUMMARY: 

2 个答案:

答案 0 :(得分:1)

我不是100%确定你要做什么,但显然,你没有正确处理递归的返回值。也许你的意思是:

function parseSummary(text, item) {
    var _text =  text || '';

    if (typeof item === 'string') {
        _text = _text + cleanupString(item);
    } else if (item.constructor == Object) {
        for (var key in item) {
          _text = parseSummary(_text, item[key]);
        }
    } else if (item.constructor == Array) {
        item.map(function(k) {
          _text = parseSummary(_text, k);
        });
    } 
    return _text; 
}

答案 1 :(得分:0)

就像你说的那样,你早早就退出了这个职能部门。你想要的可能更像是

function parseSummary(item) {
    var _text = '';

    if (typeof item === 'string') {
        _text = cleanupString(item);
    } // if item is object
    else if (item.constructor == Object) {
        for (var key in item) {
           _text += parseSummary(item[key]);
        }
    } // if item is array
    else if (item.constructor == Array) {
        item.map(function(k) {
            _text += parseSummary(k);
        });
    } 
    return _text; 
}

var input = ["\n\t  This function constructs a named and protected temporal collection \n\t  in the schema database with the specified system and valid axes.  \n\t  This function assumes that the system and valid axes already exist.  \n\t  The temporal collection is stored in the Security database.  \n\t  ", {
    "p": {
        "_children": ["A TEMPORAL-DUPCOLLECTION exception is thrown if the collection \n          already exists.  "]
    }
}, {
    "p": {
        "_children": ["\n  For details on how to create a collection, see  \n ", {
            "a": {
                "_attributes": {
                    "href": "#display.xqy?fname=http://pubs/6.0doc/xml/temporal/temporal-quick-start.xml%2366392"
                },
                "_children": ["Create a Temporal Collection"]
            }
        }, " in the ", {
            "em": {
                "_children": ["Temporal Developer's Guide"]
            }
        }]
    }
}];



function cleanupString(str) {
    return str.replace(/(\n|\t|\s+)/g, " ").trim();
}

function parseSummary(item) {
    var _text = '';
    
    if (typeof item === 'string') {
        _text = cleanupString(item);
    } // if item is object
    else if (item.constructor == Object) {
        for (var key in item) {
           _text += parseSummary(item[key]);
        }
    } // if item is array
    else if (item.constructor == Array) {
        item.map(function(k) {
            _text += parseSummary(k);
        });
    } 
    return _text; 
}
var summary = parseSummary(input);
console.log('SUMMARY:', summary); // comes out as empty