常用的Underscore取代for循环

1._.filer
    var bid_now = localStorage.getItem("bid_now");
    $scope.bid = bid_now;
    var event = JSON.parse(localStorage.getItem("bid_name_and_phone")) || [];
//    var result = [];
//    for (var i = 0; i < event.length; i++) {
//        if (bid_now == event[i].activity + event[i].bid) {
//            result.push(event[i])
//        }
//    }
//    console.log(result)
//    $scope.number = result.length;
    var result = _.filter(event, function (num) {
        return num.activity + num.bid == bid_now
    })

2._.sotBy
//    var temp;                                                                     
//    for (var i = result.length - 1; i > 0; --i) {
//        for (var j = 0; j < i; j++) {
//            if (parseInt(result[j].price) > parseInt(result[j + 1].price)) {
//                temp = result[j];
//                result[j] = result[j + 1];
//                result[j + 1] = temp;
//            }
//        }
//    }
//    $scope.lists = result;
    var result = _.sortBy(result, function (num) {
        return num.price
    })

3._.map
        for (i = 0; i < list.length; i++) {
            if (list[i].activity == (localStorage.getItem("nowActivity"))) {
                new_list.push(list[i])
            }
        }
//        var new_list = _.map(list,function(num){return num.activity == now_activity})
//        console.log(list.length + JSON.stringify(new_list))

你可能感兴趣的:(underscore)