LoginSignup
0
0

More than 3 years have passed since last update.

前の配列だったり、次の配列だったり

Posted at

前の配列を参照したり、次の配列を参照したり。
本来は同処理は吸収したほうが良いがとりあえずメモとして...。


    
    var statusDatas = {
        'list' : [{
            'data' : 'first',
            'css'  : 'firstCss'
        },{
            'data' : 'second',
            'css'  : 'secondCss'
        },{
            'data' : 'third',
            'css'  : 'thirdCss'
        },{
            'data' : 'fourth',
            'css'  : 'fourthCss'
        }],
        'next' : function(status){
            var list = this.list,
                max = list.length,
                key = 0;
            for (var i = 0; i < max; i++) {
                if (status !== list[i].data) continue;
                key = i === (max-1) ? 0 : (i + 1);
                //console.log("NEXT :", i, max, key);
                return this.list[key];
            }
            return "";
        },
        'prev' : function(status){
            var list = this.list,
                max = list.length,
                key = 0;
            for (var i = 0; i < max; i++) {
                if (status !== list[i].data) continue;
                key = i === 0 ? max-1 : i-1;
                //console.log("PREV :", i, max, key);
                return this.list[key];
            }
        }
    };
    $(function(){
        $(document).on('click', 'ul.list li', function(){
            var $this = $(this),
                status = $this.attr('data-change-test'),
                next,
                prev;
            next = statusDatas.next(status);
            prev = statusDatas.prev(status);
            
            
            console.log("NEXT:",next, "PREV:",prev);
        });
        
        
    });

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0