javascript中数组的强大用法·

1 归并

var a = [{name: 'tom'},{name: 'aiscy'},{name: 'judy'},{name: 'mike'}];
a.reduce(function(prev, item) {console.log(prev);return item.name + ' ' + prev;}, ''); // tom - aiscy - judy - mike

 2. 迭代

var children = [{name: 'father', childs: [{name: 'child1', childs: [{name: 'child2'}]}]}];

function getText(array) {
    
    arguments;
    if (array) {
        var res = array[0].name + ' - ' + arguments.callee(array[0].childs);
        return    res;
    }
    return '';

}

var s1 = getText(children);
console.log(s1); // 返回father - child1 - child2 - 

  

posted @ 2018-06-11 10:08  果感  阅读(285)  评论(0编辑  收藏  举报