0

I have created two empty array in jquery and using ajax I'm assigning the values in them suppose there are 2 values in each array assigned by the ajax but they are randomly placed but I'm populating them with the select option so for displaying it well I have to display that array in the efficient way soo here is my code I'm using but not works:-

Code:-

  <select id="dropdown">
  </select>

var array1 = Array();
var array2 = Array();
$.ajax({
    url:"/api/v1/workingspots",
    type: "GET",
    dataType: 'json',
    async: false,
    success: function(response){
        for (var i = 0; i < response.response.data.length; i++) {
            array1 = response.response.data[i].start_time;
            array2  = response.response.data[i].end_time;
            var res = array1.sort(function(a, b) {
                  return a > b
                });  
        }
    }
}); 

Error I'm facing :-

 Uncaught TypeError: array1.sort is not a function
at Object.success (booking:58)
at i (jquery.min.js:2)
at Object.fireWith [as resolveWith] (jquery.min.js:2)
at A (jquery.min.js:4)
at XMLHttpRequest.<anonymous> (jquery.min.js:4)
at Object.send (jquery.min.js:4)
at Function.ajax (jquery.min.js:4)
at HTMLDocument.<anonymous> (booking:49)
at j (jquery.min.js:2)
at k (jquery.min.js:2)

values I'm assigning to array is:-

 //By console.log(array1)
 28800
 14400

How will I sort both the arrays and assigning there each values produce by both array populate with the select option. Thank you.

1 Answer 1

2

start_time is a single value not an collection, so you need to fill the values to the collection and than apply to sort on it,

var array1 = [];

for (var i = 0; i < response.response.data.length; i++) {
    array1.push({ 
         start_time : response.response.data[i].start_time, 
         end_time: response.response.data[i].end_time
    });             
}
var res = array1.sort(function(a, b) {
   return a.start_time > b.start_time
});  
Sign up to request clarification or add additional context in comments.

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.