TypeScript之Map的遍歷

        let $cell : BeeCreateVo = null;
        let $find : BeeCreateVo = null;
        this._hash_map.forEach( ( $arr : Array<BeeCreateVo> , $key : TY_SoldierContainer , $map : any ) : void => {
            if( $arr && $arr.length > 0 ){
                for( let $i : number = 0 , $j : number = $arr.length ; $i < $j ; $i ++ ){
                    $cell = $arr[$i];
                    if( $cell.soldier_id == $id ){
                        $find = $cell;
                        break;
                    }
                }
            }
        } );

PS :
$arr : 既是Map的value
$key : 既是Map的key

注意 forEach裏面不能return , 所以 :

    public getSoliderById( $id : number , $isDeepCopy : boolean = false ) : BeeCreateVo{
        let $cell : BeeCreateVo = null;
        let $find : BeeCreateVo = null;
        this._hash_map.forEach( ( $arr : Array<BeeCreateVo> , $key : TY_SoldierContainer , $map : any ) : void => {
            if( $arr && $arr.length > 0 ){
                for( let $i : number = 0 , $j : number = $arr.length ; $i < $j ; $i ++ ){
                    $cell = $arr[$i];
                    if( $cell.soldier_id == $id ){
                        $find = $cell;
                        break;
                    }
                }
            }
        } );
        if( $find ){
            if( !$isDeepCopy ){
                return $find;
            }else{
                return {
                    id : $find.id,
                    soldier_id : $find.soldier_id,
                    soldier_num : $find.soldier_num,
                    soldier_used_num : $find.soldier_used_num,
                    technology_level : $find.technology_level,
                    time_out : $find.time_out,
                    status_code : $find.time_out
                }
            }
        }
        return null;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章