declares an array, gets the data and prints it. Then sort and print again. The first print in
debug is not consistent with the last result. I don"t understand how it works.
< body >
<button id="mybtn">getData</button>
<table id="mytable">
<tr>
<td><input></td>
<td><input type="date"></td>
<td><input type="date"></td>
</tr>
<tr>
<td><input></td>
<td><input type="date"></td>
<td><input type="date"></td>
</tr>
<tr>
<td><input></td>
<td><input type="date"></td>
<td><input type="date"></td>
</tr>
</table>
< / body >
< script src= "jquery-3.3.1.min.js" > < / script >
< script >
$(function(){
$("-sharpmybtn").click(getData);
});
function getData(){
var trs = $("-sharpmytable").find("tr");
var arr = new Array();
for(var i=0;i<trs.length;iPP){
var data1 = $(trs[i]).find("input").eq(0).val();
var data2 = $(trs[i]).find("input").eq(1).val();
var data3 = $(trs[i]).find("input").eq(2).val();
var json = {
name:data1,
time:data2,
endTime:data3
};
arr.push(json);
}
console.log("sample:");
console.log(arr);
for(var i=0;i<arr.length-1;iPP){
for(var j=0;j<arr.length-i-1;jPP){
if(arr[j].time>arr[j+1].time){
var temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
console.log("result:");
console.log(arr);
}
< / script >
The specific result of is that the array contents of console.log output under
sample and result are the same. Why does this happen?