3

I have the following webservice call which I am using to populate a dropdown menu in the UI. I am getting these records in increasing number of createDate by the webservice if I am not using the reverse() method of Array. So I decided to use it in the following manner:

$.ajax({
        beforeSend: function(request) {
            request.setRequestHeader("eppn", emailID+"@abc.com");
          },
       
        url: 'http://localhost:8080/Services/getUserByDate?userid=JACK',
        type : 'GET',
 
        dataType:'json',
        success : function(data) {
                console.log("Sept 18 : Testing getUserByDate webservice");  
                console.log(data.usersList.reverse());
                $.each(data.usersList.reverse(), function (i) { 
                $("#userSetList").append('<option><a href="#" >'+data.usersList[i].title+' | '+data.usersList[i].userSetId+' | '+moment(data.usersList[i].createDate).format('MM/DD/YYYY')+'</a></option>');
                            
                });
        },
        error : function(request,error)
        {
             $.growl.error({title: "Error", message: "Webservice request failed!" });
        }
});

And noticed the following:

console.log("Sept 18 : Testing getUserByDate webservice");  
    console.log(data.usersList.reverse());

Above code showed the items in reversed format as expected. However, when I tested the following:

$.each(data.usersList.reverse(), function (i) { 
    $("#userSetList").append('<option><a href="#" >'+data.usersList[i].title+' | '+data.usersList[i].userSetId+' | '+moment(data.usersList[i].createDate).format('MM/DD/YYYY')+'</a></option>');
});

It’s not showing items in reverse order in the UI dropdown menu. Why?

2
  • 1
    please read stackoverflow.com/help/minimal-reproducible-example Minimal, Reproducible Example Commented Sep 18, 2020 at 20:41
  • So you reverse the array in the console.log() and than you reverse() the array in the loop.... Commented Sep 18, 2020 at 20:44

2 Answers 2

3

reverse reverses an array in place and then returns it.

In your code, you reverse then array when calling console.log(data.usersList.reverse()); and then again in $.each(data.usersList.reverse(), .... Since you're reversing the same array twice, it's returned to its original order.

Instead, you can call reverse once, and then have the subsequent call use data.userList directly:

// data.userList is reversed in place
data.usersList.reverse();
console.log(data.userList);
$.each(data.usersList, function (i) { 
    $("#userSetList").append('<option><a href="#" >'+data.usersList[i].title+' | '+data.usersList[i].userSetId+' | '+moment(data.usersList[i].createDate).format('MM/DD/YYYY')+'</a></option>');        
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. In a habit to use console.log, I didn't realize that anything like that could happen.
2

array.reverse() as no problem.

const
  mySelect = document.getElementById('my-select')
, data =
    [ { val:'a', lib:'a_lib' }
    , { val:'b', lib:'b_lib' }
    , { val:'c', lib:'c_lib' }
    , { val:'d', lib:'d_lib' }
    ]
    
data.reverse().forEach(el=>{ mySelect.add( new Option(el.lib, el.val)) })
<select id="my-select"></select>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.