Return arrays in Javascript












2















I'm using the Dragsort plugin and we want to tag IDs in an array and print them.



My Html Code Is:



<ul id="list1">
<li class="ss"><div>1</div></li>
<li><div>2</div></li>
<li><div>3</div></li>
<li><div>4</div></li>
<li><div>5</div></li>
<li><div>6</div></li>
<li><div>7</div></li>
<li><div>8</div></li>
<li><div>9</div></li>
</ul>
<p id="demo"></p>


and my jQyery codes is



$("#list1, #list2").dragsort({ dragSelector: "div", dragBetween: true, dragEnd: saveOrder, placeHolderTemplate: "<li class='placeHolder'><div></div></li>" });

function saveOrder() {
var data = $("#list1 li").map(function() { return $(this).children().html(); }).get();
$("input[name=list1SortOrder]").val(data.join("|"));
};

$( "#list1 li" ).each(function( index ) {
let items = ;
items = [ index + 1];
console.log(items.map(() => index));
$(this).attr("id",items);
});

$('#hamid').click(function () {
$( "#list1 li" ).each(function() {
its = [ $(this).attr('id')];
});
});


When I want to return id <li> return last of them










share|improve this question

























  • The question is not clear. What is the problem you are facing?

    – Ahmad
    Nov 14 '18 at 10:09











  • problem is when i want to return all ids in "its" variable , it return just last id

    – Arman Bagheri
    Nov 14 '18 at 10:11











  • You're reassigning its with a new array when iterating through the whole collection of #list1 li. Just use .map() to return the array of IDs in all the <li> elements in the click handler.

    – Terry
    Nov 14 '18 at 10:13













  • can you give me code it ?

    – Arman Bagheri
    Nov 14 '18 at 10:15











  • @arman Sure. See my answer: there are two ways to do it: a basic way, and a truly jQuery way :)

    – Terry
    Nov 14 '18 at 10:19
















2















I'm using the Dragsort plugin and we want to tag IDs in an array and print them.



My Html Code Is:



<ul id="list1">
<li class="ss"><div>1</div></li>
<li><div>2</div></li>
<li><div>3</div></li>
<li><div>4</div></li>
<li><div>5</div></li>
<li><div>6</div></li>
<li><div>7</div></li>
<li><div>8</div></li>
<li><div>9</div></li>
</ul>
<p id="demo"></p>


and my jQyery codes is



$("#list1, #list2").dragsort({ dragSelector: "div", dragBetween: true, dragEnd: saveOrder, placeHolderTemplate: "<li class='placeHolder'><div></div></li>" });

function saveOrder() {
var data = $("#list1 li").map(function() { return $(this).children().html(); }).get();
$("input[name=list1SortOrder]").val(data.join("|"));
};

$( "#list1 li" ).each(function( index ) {
let items = ;
items = [ index + 1];
console.log(items.map(() => index));
$(this).attr("id",items);
});

$('#hamid').click(function () {
$( "#list1 li" ).each(function() {
its = [ $(this).attr('id')];
});
});


When I want to return id <li> return last of them










share|improve this question

























  • The question is not clear. What is the problem you are facing?

    – Ahmad
    Nov 14 '18 at 10:09











  • problem is when i want to return all ids in "its" variable , it return just last id

    – Arman Bagheri
    Nov 14 '18 at 10:11











  • You're reassigning its with a new array when iterating through the whole collection of #list1 li. Just use .map() to return the array of IDs in all the <li> elements in the click handler.

    – Terry
    Nov 14 '18 at 10:13













  • can you give me code it ?

    – Arman Bagheri
    Nov 14 '18 at 10:15











  • @arman Sure. See my answer: there are two ways to do it: a basic way, and a truly jQuery way :)

    – Terry
    Nov 14 '18 at 10:19














2












2








2


0






I'm using the Dragsort plugin and we want to tag IDs in an array and print them.



My Html Code Is:



<ul id="list1">
<li class="ss"><div>1</div></li>
<li><div>2</div></li>
<li><div>3</div></li>
<li><div>4</div></li>
<li><div>5</div></li>
<li><div>6</div></li>
<li><div>7</div></li>
<li><div>8</div></li>
<li><div>9</div></li>
</ul>
<p id="demo"></p>


and my jQyery codes is



$("#list1, #list2").dragsort({ dragSelector: "div", dragBetween: true, dragEnd: saveOrder, placeHolderTemplate: "<li class='placeHolder'><div></div></li>" });

function saveOrder() {
var data = $("#list1 li").map(function() { return $(this).children().html(); }).get();
$("input[name=list1SortOrder]").val(data.join("|"));
};

$( "#list1 li" ).each(function( index ) {
let items = ;
items = [ index + 1];
console.log(items.map(() => index));
$(this).attr("id",items);
});

$('#hamid').click(function () {
$( "#list1 li" ).each(function() {
its = [ $(this).attr('id')];
});
});


When I want to return id <li> return last of them










share|improve this question
















I'm using the Dragsort plugin and we want to tag IDs in an array and print them.



My Html Code Is:



<ul id="list1">
<li class="ss"><div>1</div></li>
<li><div>2</div></li>
<li><div>3</div></li>
<li><div>4</div></li>
<li><div>5</div></li>
<li><div>6</div></li>
<li><div>7</div></li>
<li><div>8</div></li>
<li><div>9</div></li>
</ul>
<p id="demo"></p>


and my jQyery codes is



$("#list1, #list2").dragsort({ dragSelector: "div", dragBetween: true, dragEnd: saveOrder, placeHolderTemplate: "<li class='placeHolder'><div></div></li>" });

function saveOrder() {
var data = $("#list1 li").map(function() { return $(this).children().html(); }).get();
$("input[name=list1SortOrder]").val(data.join("|"));
};

$( "#list1 li" ).each(function( index ) {
let items = ;
items = [ index + 1];
console.log(items.map(() => index));
$(this).attr("id",items);
});

$('#hamid').click(function () {
$( "#list1 li" ).each(function() {
its = [ $(this).attr('id')];
});
});


When I want to return id <li> return last of them







javascript jquery






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 10:13









executable

1,7602822




1,7602822










asked Nov 14 '18 at 10:05









Arman BagheriArman Bagheri

1499




1499













  • The question is not clear. What is the problem you are facing?

    – Ahmad
    Nov 14 '18 at 10:09











  • problem is when i want to return all ids in "its" variable , it return just last id

    – Arman Bagheri
    Nov 14 '18 at 10:11











  • You're reassigning its with a new array when iterating through the whole collection of #list1 li. Just use .map() to return the array of IDs in all the <li> elements in the click handler.

    – Terry
    Nov 14 '18 at 10:13













  • can you give me code it ?

    – Arman Bagheri
    Nov 14 '18 at 10:15











  • @arman Sure. See my answer: there are two ways to do it: a basic way, and a truly jQuery way :)

    – Terry
    Nov 14 '18 at 10:19



















  • The question is not clear. What is the problem you are facing?

    – Ahmad
    Nov 14 '18 at 10:09











  • problem is when i want to return all ids in "its" variable , it return just last id

    – Arman Bagheri
    Nov 14 '18 at 10:11











  • You're reassigning its with a new array when iterating through the whole collection of #list1 li. Just use .map() to return the array of IDs in all the <li> elements in the click handler.

    – Terry
    Nov 14 '18 at 10:13













  • can you give me code it ?

    – Arman Bagheri
    Nov 14 '18 at 10:15











  • @arman Sure. See my answer: there are two ways to do it: a basic way, and a truly jQuery way :)

    – Terry
    Nov 14 '18 at 10:19

















The question is not clear. What is the problem you are facing?

– Ahmad
Nov 14 '18 at 10:09





The question is not clear. What is the problem you are facing?

– Ahmad
Nov 14 '18 at 10:09













problem is when i want to return all ids in "its" variable , it return just last id

– Arman Bagheri
Nov 14 '18 at 10:11





problem is when i want to return all ids in "its" variable , it return just last id

– Arman Bagheri
Nov 14 '18 at 10:11













You're reassigning its with a new array when iterating through the whole collection of #list1 li. Just use .map() to return the array of IDs in all the <li> elements in the click handler.

– Terry
Nov 14 '18 at 10:13







You're reassigning its with a new array when iterating through the whole collection of #list1 li. Just use .map() to return the array of IDs in all the <li> elements in the click handler.

– Terry
Nov 14 '18 at 10:13















can you give me code it ?

– Arman Bagheri
Nov 14 '18 at 10:15





can you give me code it ?

– Arman Bagheri
Nov 14 '18 at 10:15













@arman Sure. See my answer: there are two ways to do it: a basic way, and a truly jQuery way :)

– Terry
Nov 14 '18 at 10:19





@arman Sure. See my answer: there are two ways to do it: a basic way, and a truly jQuery way :)

– Terry
Nov 14 '18 at 10:19












2 Answers
2






active

oldest

votes


















2














You may want to change the line



its = [ $(this).attr('id')];
//this will only create an instance of an array with one element.


change the last function to something like:



$('#hamid').click(function () {
var ids = ;
$( "#list1 li" ).each(function() {
ids.push( $(this).attr('id') );
});
console.log(ids);
// this will print an array with all ids inside
// then you can return it or do whatever you want with it
});





share|improve this answer


























  • thanks it worked

    – Arman Bagheri
    Nov 14 '18 at 10:16











  • yes of course..

    – Arman Bagheri
    Nov 14 '18 at 11:20



















1














I guess your issue is that you want to get all the IDs of the elements that matches #list1 li, as seen in the last 5 lines of your code. The reason why it is not working is because you are reassigning a brand new array to the its variable. I suggest doing one of the two:




  • Solution 1: Push into the array instead of reassigning it

  • Solution 2: Use .map() + .get() to return an array of all IDs




Solution 1 (basic)



Use Array.prototype.push() to append IDs to a pre-existing array. Remember that instead of converting this into a jQuery object and then accessing its ID via the .attr() method (by doing $(this).attr('id')), you can easily do this.id:



$('#hamid').click(function () {
var its = ;
$('#list1 li').each(function() {
its.push(this.id);
});
console.log(its);
});




Solution 2 (better)



Use a combination of .map() and .get(). This is the most jQuery-way to do it:



$('#hamid').click(function () {
var its = $('#list1 li').map(function() {
return this.id;
}).get();
console.log(its);
});


The reason why .get() is needed is because .map() returns a jQuery collection instead of an actual array. To retrieve the array itself, you will need to append .get() after .map().






share|improve this answer























    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%2f53297568%2freturn-arrays-in-javascript%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









    2














    You may want to change the line



    its = [ $(this).attr('id')];
    //this will only create an instance of an array with one element.


    change the last function to something like:



    $('#hamid').click(function () {
    var ids = ;
    $( "#list1 li" ).each(function() {
    ids.push( $(this).attr('id') );
    });
    console.log(ids);
    // this will print an array with all ids inside
    // then you can return it or do whatever you want with it
    });





    share|improve this answer


























    • thanks it worked

      – Arman Bagheri
      Nov 14 '18 at 10:16











    • yes of course..

      – Arman Bagheri
      Nov 14 '18 at 11:20
















    2














    You may want to change the line



    its = [ $(this).attr('id')];
    //this will only create an instance of an array with one element.


    change the last function to something like:



    $('#hamid').click(function () {
    var ids = ;
    $( "#list1 li" ).each(function() {
    ids.push( $(this).attr('id') );
    });
    console.log(ids);
    // this will print an array with all ids inside
    // then you can return it or do whatever you want with it
    });





    share|improve this answer


























    • thanks it worked

      – Arman Bagheri
      Nov 14 '18 at 10:16











    • yes of course..

      – Arman Bagheri
      Nov 14 '18 at 11:20














    2












    2








    2







    You may want to change the line



    its = [ $(this).attr('id')];
    //this will only create an instance of an array with one element.


    change the last function to something like:



    $('#hamid').click(function () {
    var ids = ;
    $( "#list1 li" ).each(function() {
    ids.push( $(this).attr('id') );
    });
    console.log(ids);
    // this will print an array with all ids inside
    // then you can return it or do whatever you want with it
    });





    share|improve this answer















    You may want to change the line



    its = [ $(this).attr('id')];
    //this will only create an instance of an array with one element.


    change the last function to something like:



    $('#hamid').click(function () {
    var ids = ;
    $( "#list1 li" ).each(function() {
    ids.push( $(this).attr('id') );
    });
    console.log(ids);
    // this will print an array with all ids inside
    // then you can return it or do whatever you want with it
    });






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 14 '18 at 10:17

























    answered Nov 14 '18 at 10:15









    AhmadAhmad

    8,23543563




    8,23543563













    • thanks it worked

      – Arman Bagheri
      Nov 14 '18 at 10:16











    • yes of course..

      – Arman Bagheri
      Nov 14 '18 at 11:20



















    • thanks it worked

      – Arman Bagheri
      Nov 14 '18 at 10:16











    • yes of course..

      – Arman Bagheri
      Nov 14 '18 at 11:20

















    thanks it worked

    – Arman Bagheri
    Nov 14 '18 at 10:16





    thanks it worked

    – Arman Bagheri
    Nov 14 '18 at 10:16













    yes of course..

    – Arman Bagheri
    Nov 14 '18 at 11:20





    yes of course..

    – Arman Bagheri
    Nov 14 '18 at 11:20













    1














    I guess your issue is that you want to get all the IDs of the elements that matches #list1 li, as seen in the last 5 lines of your code. The reason why it is not working is because you are reassigning a brand new array to the its variable. I suggest doing one of the two:




    • Solution 1: Push into the array instead of reassigning it

    • Solution 2: Use .map() + .get() to return an array of all IDs




    Solution 1 (basic)



    Use Array.prototype.push() to append IDs to a pre-existing array. Remember that instead of converting this into a jQuery object and then accessing its ID via the .attr() method (by doing $(this).attr('id')), you can easily do this.id:



    $('#hamid').click(function () {
    var its = ;
    $('#list1 li').each(function() {
    its.push(this.id);
    });
    console.log(its);
    });




    Solution 2 (better)



    Use a combination of .map() and .get(). This is the most jQuery-way to do it:



    $('#hamid').click(function () {
    var its = $('#list1 li').map(function() {
    return this.id;
    }).get();
    console.log(its);
    });


    The reason why .get() is needed is because .map() returns a jQuery collection instead of an actual array. To retrieve the array itself, you will need to append .get() after .map().






    share|improve this answer




























      1














      I guess your issue is that you want to get all the IDs of the elements that matches #list1 li, as seen in the last 5 lines of your code. The reason why it is not working is because you are reassigning a brand new array to the its variable. I suggest doing one of the two:




      • Solution 1: Push into the array instead of reassigning it

      • Solution 2: Use .map() + .get() to return an array of all IDs




      Solution 1 (basic)



      Use Array.prototype.push() to append IDs to a pre-existing array. Remember that instead of converting this into a jQuery object and then accessing its ID via the .attr() method (by doing $(this).attr('id')), you can easily do this.id:



      $('#hamid').click(function () {
      var its = ;
      $('#list1 li').each(function() {
      its.push(this.id);
      });
      console.log(its);
      });




      Solution 2 (better)



      Use a combination of .map() and .get(). This is the most jQuery-way to do it:



      $('#hamid').click(function () {
      var its = $('#list1 li').map(function() {
      return this.id;
      }).get();
      console.log(its);
      });


      The reason why .get() is needed is because .map() returns a jQuery collection instead of an actual array. To retrieve the array itself, you will need to append .get() after .map().






      share|improve this answer


























        1












        1








        1







        I guess your issue is that you want to get all the IDs of the elements that matches #list1 li, as seen in the last 5 lines of your code. The reason why it is not working is because you are reassigning a brand new array to the its variable. I suggest doing one of the two:




        • Solution 1: Push into the array instead of reassigning it

        • Solution 2: Use .map() + .get() to return an array of all IDs




        Solution 1 (basic)



        Use Array.prototype.push() to append IDs to a pre-existing array. Remember that instead of converting this into a jQuery object and then accessing its ID via the .attr() method (by doing $(this).attr('id')), you can easily do this.id:



        $('#hamid').click(function () {
        var its = ;
        $('#list1 li').each(function() {
        its.push(this.id);
        });
        console.log(its);
        });




        Solution 2 (better)



        Use a combination of .map() and .get(). This is the most jQuery-way to do it:



        $('#hamid').click(function () {
        var its = $('#list1 li').map(function() {
        return this.id;
        }).get();
        console.log(its);
        });


        The reason why .get() is needed is because .map() returns a jQuery collection instead of an actual array. To retrieve the array itself, you will need to append .get() after .map().






        share|improve this answer













        I guess your issue is that you want to get all the IDs of the elements that matches #list1 li, as seen in the last 5 lines of your code. The reason why it is not working is because you are reassigning a brand new array to the its variable. I suggest doing one of the two:




        • Solution 1: Push into the array instead of reassigning it

        • Solution 2: Use .map() + .get() to return an array of all IDs




        Solution 1 (basic)



        Use Array.prototype.push() to append IDs to a pre-existing array. Remember that instead of converting this into a jQuery object and then accessing its ID via the .attr() method (by doing $(this).attr('id')), you can easily do this.id:



        $('#hamid').click(function () {
        var its = ;
        $('#list1 li').each(function() {
        its.push(this.id);
        });
        console.log(its);
        });




        Solution 2 (better)



        Use a combination of .map() and .get(). This is the most jQuery-way to do it:



        $('#hamid').click(function () {
        var its = $('#list1 li').map(function() {
        return this.id;
        }).get();
        console.log(its);
        });


        The reason why .get() is needed is because .map() returns a jQuery collection instead of an actual array. To retrieve the array itself, you will need to append .get() after .map().







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 10:17









        TerryTerry

        29k44268




        29k44268






























            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%2f53297568%2freturn-arrays-in-javascript%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







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python