美文网首页
2018-09-30 nodejs生成指定数据范围的数组用于as

2018-09-30 nodejs生成指定数据范围的数组用于as

作者: 五大RobertWu伍洋 | 来源:发表于2018-09-30 17:49 被阅读80次
const min = req.query.creative_id_min;
const max = req.query.creative_id_max;
var idarrays = [];
debug("test range id array:", Array.from(new Range(1, 8, 2)));
debug("min:", min, " Max:", max);
debug("test range id array 2:", [Array.from(new Range(min, max, 1))]);
for (var id = min; id <= max; id++) {
    idarrays.push(id);
}
debug('idarray:', idarrays);

注意:

Array.from(new Range(min, max, 1)) 可以取代for循环生成指定数字范围的数组了!

Range对象里使用了parseInt()处理旻min,max(因为来自http get请求)

Range类定义如下:

   class Range {
        constructor(start, end, step) {
                this.start = parseInt(start)
                this.end = parseInt(end)
                this.step = parseInt(step)
            }
            [Symbol.iterator]() {
                let curr = this.start,
                    _this = this
                    //  必须要添加*号
                return function*() {
                        //  必须要使用循环
                        while (curr <= _this.end) {
                            yield curr
                            curr = curr + _this.step
                        }
                    }() //  必须要对此函数进行执行
            }
    }

    module.exports = Range;

相关文章

网友评论

      本文标题:2018-09-30 nodejs生成指定数据范围的数组用于as

      本文链接:https://www.haomeiwen.com/subject/jmfwoftx.html