When push a data into array is pushing in just one index










0















Hi there guys I am new in this but I have a array in my localstorage I get that array I save in old empty array, and I make a petition to the ajax and I try push the new data into my old array



$.getJSON(setUrl(pagination, page), function (response) 
var old = ;
old = JSON.parse(localStorage.getItem('msg'));
old.push(response.DATA);
console.log(old);
);


but the result I getting is some like this



enter image description here



I put all my new array inside a index of my old array what am I doing wrong ? can some one help me










share|improve this question






















  • This happens when response.DATA is an array. This is behaving as expected. If you want to add each item individually, iterate over them.

    – Bucket
    Nov 15 '18 at 16:16











  • possible duplicate of stackoverflow.com/questions/1374126/…

    – Tanmay
    Nov 15 '18 at 16:33
















0















Hi there guys I am new in this but I have a array in my localstorage I get that array I save in old empty array, and I make a petition to the ajax and I try push the new data into my old array



$.getJSON(setUrl(pagination, page), function (response) 
var old = ;
old = JSON.parse(localStorage.getItem('msg'));
old.push(response.DATA);
console.log(old);
);


but the result I getting is some like this



enter image description here



I put all my new array inside a index of my old array what am I doing wrong ? can some one help me










share|improve this question






















  • This happens when response.DATA is an array. This is behaving as expected. If you want to add each item individually, iterate over them.

    – Bucket
    Nov 15 '18 at 16:16











  • possible duplicate of stackoverflow.com/questions/1374126/…

    – Tanmay
    Nov 15 '18 at 16:33














0












0








0








Hi there guys I am new in this but I have a array in my localstorage I get that array I save in old empty array, and I make a petition to the ajax and I try push the new data into my old array



$.getJSON(setUrl(pagination, page), function (response) 
var old = ;
old = JSON.parse(localStorage.getItem('msg'));
old.push(response.DATA);
console.log(old);
);


but the result I getting is some like this



enter image description here



I put all my new array inside a index of my old array what am I doing wrong ? can some one help me










share|improve this question














Hi there guys I am new in this but I have a array in my localstorage I get that array I save in old empty array, and I make a petition to the ajax and I try push the new data into my old array



$.getJSON(setUrl(pagination, page), function (response) 
var old = ;
old = JSON.parse(localStorage.getItem('msg'));
old.push(response.DATA);
console.log(old);
);


but the result I getting is some like this



enter image description here



I put all my new array inside a index of my old array what am I doing wrong ? can some one help me







javascript jquery arrays






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 16:13









Alfredo IzquierdoAlfredo Izquierdo

287




287












  • This happens when response.DATA is an array. This is behaving as expected. If you want to add each item individually, iterate over them.

    – Bucket
    Nov 15 '18 at 16:16











  • possible duplicate of stackoverflow.com/questions/1374126/…

    – Tanmay
    Nov 15 '18 at 16:33


















  • This happens when response.DATA is an array. This is behaving as expected. If you want to add each item individually, iterate over them.

    – Bucket
    Nov 15 '18 at 16:16











  • possible duplicate of stackoverflow.com/questions/1374126/…

    – Tanmay
    Nov 15 '18 at 16:33

















This happens when response.DATA is an array. This is behaving as expected. If you want to add each item individually, iterate over them.

– Bucket
Nov 15 '18 at 16:16





This happens when response.DATA is an array. This is behaving as expected. If you want to add each item individually, iterate over them.

– Bucket
Nov 15 '18 at 16:16













possible duplicate of stackoverflow.com/questions/1374126/…

– Tanmay
Nov 15 '18 at 16:33






possible duplicate of stackoverflow.com/questions/1374126/…

– Tanmay
Nov 15 '18 at 16:33













2 Answers
2






active

oldest

votes


















1














You have to use concat but remember....




The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array. source




$.getJSON(setUrl(pagination, page), function (response) 
var old = ;
old = JSON.parse(localStorage.getItem('msg'));
var combined = old.concat(response.DATA);
console.log(combined);
);





share|improve this answer






























    1














    Use Array.ptototype.concat() instead to merge the arrays.
    Array.prototype.concat():




    The concat() method is used to merge two or more arrays. This method
    does not change the existing arrays, but instead returns a new array.




    $.getJSON(setUrl(pagination, page), function (response) 
    var old = ;
    old = JSON.parse(localStorage.getItem('msg'));
    old = old.concat(response.DATA);
    console.log(old);
    );





    var old = [
    id: 1 ,
    id: 2
    ];

    console.log('old: ', old);

    var json = [
    id: 3 ,
    id: 4
    ];

    old = old.concat(json);

    console.log('merged: ', old);





    Hopefully that helps!






    share|improve this answer

























    • I use concat before but the element return is the same array

      – Alfredo Izquierdo
      Nov 15 '18 at 16:19












    • I try save each element as a new element in the old array

      – Alfredo Izquierdo
      Nov 15 '18 at 16:20











    • You are right, I missed a key portion in my answer. concat() returns a new array so it would need to be saved to a variable. Updated the answer.

      – Alexander Staroselsky
      Nov 15 '18 at 16:21











    • @AlfredoIzquierdo You can reassign the value of old if the goal is to specifically use old again in the method or elsewhere. I've updated the answer to reflect that.

      – Alexander Staroselsky
      Nov 15 '18 at 16:40










    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323587%2fwhen-push-a-data-into-array-is-pushing-in-just-one-index%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You have to use concat but remember....




    The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array. source




    $.getJSON(setUrl(pagination, page), function (response) 
    var old = ;
    old = JSON.parse(localStorage.getItem('msg'));
    var combined = old.concat(response.DATA);
    console.log(combined);
    );





    share|improve this answer



























      1














      You have to use concat but remember....




      The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array. source




      $.getJSON(setUrl(pagination, page), function (response) 
      var old = ;
      old = JSON.parse(localStorage.getItem('msg'));
      var combined = old.concat(response.DATA);
      console.log(combined);
      );





      share|improve this answer

























        1












        1








        1







        You have to use concat but remember....




        The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array. source




        $.getJSON(setUrl(pagination, page), function (response) 
        var old = ;
        old = JSON.parse(localStorage.getItem('msg'));
        var combined = old.concat(response.DATA);
        console.log(combined);
        );





        share|improve this answer













        You have to use concat but remember....




        The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array. source




        $.getJSON(setUrl(pagination, page), function (response) 
        var old = ;
        old = JSON.parse(localStorage.getItem('msg'));
        var combined = old.concat(response.DATA);
        console.log(combined);
        );






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 16:20









        JamiecJamiec

        94.8k1193152




        94.8k1193152























            1














            Use Array.ptototype.concat() instead to merge the arrays.
            Array.prototype.concat():




            The concat() method is used to merge two or more arrays. This method
            does not change the existing arrays, but instead returns a new array.




            $.getJSON(setUrl(pagination, page), function (response) 
            var old = ;
            old = JSON.parse(localStorage.getItem('msg'));
            old = old.concat(response.DATA);
            console.log(old);
            );





            var old = [
            id: 1 ,
            id: 2
            ];

            console.log('old: ', old);

            var json = [
            id: 3 ,
            id: 4
            ];

            old = old.concat(json);

            console.log('merged: ', old);





            Hopefully that helps!






            share|improve this answer

























            • I use concat before but the element return is the same array

              – Alfredo Izquierdo
              Nov 15 '18 at 16:19












            • I try save each element as a new element in the old array

              – Alfredo Izquierdo
              Nov 15 '18 at 16:20











            • You are right, I missed a key portion in my answer. concat() returns a new array so it would need to be saved to a variable. Updated the answer.

              – Alexander Staroselsky
              Nov 15 '18 at 16:21











            • @AlfredoIzquierdo You can reassign the value of old if the goal is to specifically use old again in the method or elsewhere. I've updated the answer to reflect that.

              – Alexander Staroselsky
              Nov 15 '18 at 16:40















            1














            Use Array.ptototype.concat() instead to merge the arrays.
            Array.prototype.concat():




            The concat() method is used to merge two or more arrays. This method
            does not change the existing arrays, but instead returns a new array.




            $.getJSON(setUrl(pagination, page), function (response) 
            var old = ;
            old = JSON.parse(localStorage.getItem('msg'));
            old = old.concat(response.DATA);
            console.log(old);
            );





            var old = [
            id: 1 ,
            id: 2
            ];

            console.log('old: ', old);

            var json = [
            id: 3 ,
            id: 4
            ];

            old = old.concat(json);

            console.log('merged: ', old);





            Hopefully that helps!






            share|improve this answer

























            • I use concat before but the element return is the same array

              – Alfredo Izquierdo
              Nov 15 '18 at 16:19












            • I try save each element as a new element in the old array

              – Alfredo Izquierdo
              Nov 15 '18 at 16:20











            • You are right, I missed a key portion in my answer. concat() returns a new array so it would need to be saved to a variable. Updated the answer.

              – Alexander Staroselsky
              Nov 15 '18 at 16:21











            • @AlfredoIzquierdo You can reassign the value of old if the goal is to specifically use old again in the method or elsewhere. I've updated the answer to reflect that.

              – Alexander Staroselsky
              Nov 15 '18 at 16:40













            1












            1








            1







            Use Array.ptototype.concat() instead to merge the arrays.
            Array.prototype.concat():




            The concat() method is used to merge two or more arrays. This method
            does not change the existing arrays, but instead returns a new array.




            $.getJSON(setUrl(pagination, page), function (response) 
            var old = ;
            old = JSON.parse(localStorage.getItem('msg'));
            old = old.concat(response.DATA);
            console.log(old);
            );





            var old = [
            id: 1 ,
            id: 2
            ];

            console.log('old: ', old);

            var json = [
            id: 3 ,
            id: 4
            ];

            old = old.concat(json);

            console.log('merged: ', old);





            Hopefully that helps!






            share|improve this answer















            Use Array.ptototype.concat() instead to merge the arrays.
            Array.prototype.concat():




            The concat() method is used to merge two or more arrays. This method
            does not change the existing arrays, but instead returns a new array.




            $.getJSON(setUrl(pagination, page), function (response) 
            var old = ;
            old = JSON.parse(localStorage.getItem('msg'));
            old = old.concat(response.DATA);
            console.log(old);
            );





            var old = [
            id: 1 ,
            id: 2
            ];

            console.log('old: ', old);

            var json = [
            id: 3 ,
            id: 4
            ];

            old = old.concat(json);

            console.log('merged: ', old);





            Hopefully that helps!






            var old = [
            id: 1 ,
            id: 2
            ];

            console.log('old: ', old);

            var json = [
            id: 3 ,
            id: 4
            ];

            old = old.concat(json);

            console.log('merged: ', old);





            var old = [
            id: 1 ,
            id: 2
            ];

            console.log('old: ', old);

            var json = [
            id: 3 ,
            id: 4
            ];

            old = old.concat(json);

            console.log('merged: ', old);






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 15 '18 at 16:28

























            answered Nov 15 '18 at 16:16









            Alexander StaroselskyAlexander Staroselsky

            13.7k42342




            13.7k42342












            • I use concat before but the element return is the same array

              – Alfredo Izquierdo
              Nov 15 '18 at 16:19












            • I try save each element as a new element in the old array

              – Alfredo Izquierdo
              Nov 15 '18 at 16:20











            • You are right, I missed a key portion in my answer. concat() returns a new array so it would need to be saved to a variable. Updated the answer.

              – Alexander Staroselsky
              Nov 15 '18 at 16:21











            • @AlfredoIzquierdo You can reassign the value of old if the goal is to specifically use old again in the method or elsewhere. I've updated the answer to reflect that.

              – Alexander Staroselsky
              Nov 15 '18 at 16:40

















            • I use concat before but the element return is the same array

              – Alfredo Izquierdo
              Nov 15 '18 at 16:19












            • I try save each element as a new element in the old array

              – Alfredo Izquierdo
              Nov 15 '18 at 16:20











            • You are right, I missed a key portion in my answer. concat() returns a new array so it would need to be saved to a variable. Updated the answer.

              – Alexander Staroselsky
              Nov 15 '18 at 16:21











            • @AlfredoIzquierdo You can reassign the value of old if the goal is to specifically use old again in the method or elsewhere. I've updated the answer to reflect that.

              – Alexander Staroselsky
              Nov 15 '18 at 16:40
















            I use concat before but the element return is the same array

            – Alfredo Izquierdo
            Nov 15 '18 at 16:19






            I use concat before but the element return is the same array

            – Alfredo Izquierdo
            Nov 15 '18 at 16:19














            I try save each element as a new element in the old array

            – Alfredo Izquierdo
            Nov 15 '18 at 16:20





            I try save each element as a new element in the old array

            – Alfredo Izquierdo
            Nov 15 '18 at 16:20













            You are right, I missed a key portion in my answer. concat() returns a new array so it would need to be saved to a variable. Updated the answer.

            – Alexander Staroselsky
            Nov 15 '18 at 16:21





            You are right, I missed a key portion in my answer. concat() returns a new array so it would need to be saved to a variable. Updated the answer.

            – Alexander Staroselsky
            Nov 15 '18 at 16:21













            @AlfredoIzquierdo You can reassign the value of old if the goal is to specifically use old again in the method or elsewhere. I've updated the answer to reflect that.

            – Alexander Staroselsky
            Nov 15 '18 at 16:40





            @AlfredoIzquierdo You can reassign the value of old if the goal is to specifically use old again in the method or elsewhere. I've updated the answer to reflect that.

            – Alexander Staroselsky
            Nov 15 '18 at 16:40

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323587%2fwhen-push-a-data-into-array-is-pushing-in-just-one-index%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto