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

javascript天然的迭代器_javascript技巧

WBOY
Release: 2016-05-16 18:17:28
Original
1210 people have browsed it

它源于群里的某一题目:

有一个数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; 
}
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template