最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript array.push() do not add but replace it - Stack Overflow

programmeradmin4浏览0评论

I have some checkboxes styled with bootstrapSwitch. I wrote a script that have to add value of checkbox to an array when bootstrapSwitch state is true.

This is my code :

$('input[name^=skill]').on('switchChange.bootstrapSwitch', function (event, state) {

    //alert(state);
    //var s = [];

    var s = new Array();

    if( state === true )
    {
        //var value = $(this).val();
        s.push( $(this).val() );

        console.log(s);//(value)

    }

        console.log(s);//(value)
});

But surprisingly push method replace the value and my s array always have one index.

Would you please let me know why is that?

Thanks in Advance

I have some checkboxes styled with bootstrapSwitch. I wrote a script that have to add value of checkbox to an array when bootstrapSwitch state is true.

This is my code :

$('input[name^=skill]').on('switchChange.bootstrapSwitch', function (event, state) {

    //alert(state);
    //var s = [];

    var s = new Array();

    if( state === true )
    {
        //var value = $(this).val();
        s.push( $(this).val() );

        console.log(s);//(value)

    }

        console.log(s);//(value)
});

But surprisingly push method replace the value and my s array always have one index.

Would you please let me know why is that?

Thanks in Advance

Share Improve this question asked Apr 18, 2016 at 10:04 KiyarashKiyarash 2,5888 gold badges36 silver badges61 bronze badges 2
  • Define array out of the handler..Or it will be initialized to [](empty-array) every time handler is invoked... – Rayon Commented Apr 18, 2016 at 10:06
  • 2 That's because you're re-creating s upon each click. Try to move the declaration of s outside of the event callback – haim770 Commented Apr 18, 2016 at 10:06
Add a ment  | 

3 Answers 3

Reset to default 6
var s = new Array();

Define this out of the handler function.

It's always 1 because you're recreating it everytime.

var s = new Array();// this line should be out side of function. so it will not be new object everytime

so try this

var s = new Array();// this line should be here. so it will not be new object everytime

$('input[name^=skill]').on('switchChange.bootstrapSwitch', function (event, state) {

    //alert(state);
    //var s = [];

    var s = new Array();

    if( state === true )
    {
        //var value = $(this).val();
        s.push( $(this).val() );

        console.log(s);//(value)

    }

        console.log(s);//(value)
});
var s = [];

$('input[name^=skill]').on('switchChange.bootstrapSwitch', function (event, state) {

        //alert(state);
        //var s = [];



        if( state === true )
        {
            //var value = $(this).val();
            s.push( $(this).val() );

            console.log(s);//(value)

        }

            console.log(s);//(value)
    });

Just declare the array outside. User [] instead on new Array() as it is faster.

发布评论

评论列表(0)

  1. 暂无评论