Add to Array jQuery

后端 未结 4 1408
一生所求
一生所求 2020-12-07 21:32

I know how to initliaize one but how do add I items to an Array? I heard it was push() maybe? I can\'t find it...

相关标签:
4条回答
  • 2020-12-07 22:14

    push is a native javascript method. You could use it like this:

    var array = [1, 2, 3];
    array.push(4); // array now is [1, 2, 3, 4]
    array.push(5, 6, 7); // array now is [1, 2, 3, 4, 5, 6, 7]
    
    0 讨论(0)
  • 2020-12-07 22:14

    You are right. This has nothing to do with jQuery though.

    var myArray = [];
    myArray.push("foo");
    // myArray now contains "foo" at index 0.
    
    0 讨论(0)
  • 2020-12-07 22:21

    For JavaScript arrays, you use push().

    var a = [];
    a.push(12);
    a.push(32);
    

    For jQuery objects, there's add().

    $('div.test').add('p.blue');
    

    Note that while push() modifies the original array in-place, add() returns a new jQuery object, it does not modify the original one.

    0 讨论(0)
  • 2020-12-07 22:29

    For JavaScript arrays, you use Both push() and concat() function.

    var array = [1, 2, 3];
    array.push(4, 5);         //use push for appending a single array.
    
    
    
    
    var array1 = [1, 2, 3];
    var array2 = [4, 5, 6];
    
    var array3 = array1.concat(array2);   //It is better use concat for appending more then one array.
    
    0 讨论(0)
提交回复
热议问题