Home >Web Front-end >JS Tutorial >javascript天然的迭代器_javascript技巧

javascript天然的迭代器_javascript技巧

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 18:17:281262browse

它源于群里的某一题目:

有一个数n=5,不用for循环,怎么返回[1,2,3,4,5]这样一个数组
群的“糖果男孩”给的答案:

复制代码 代码如下:

function getArr(n) { 
    var a = [], 
        b = [], 
        m = 0; 
    a.length = n + 1; 
    a.toString().replace(/,/g, function () { 
        b.push(++m); 
    }); 
    return b; 
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn