json 날짜별로 출력
본문
API를 통해 리스트를 뿌려주는데 날짜별로 정렬하고자합니다 새로고침때마다 랜덤으로 출력되고 있습니다
날짜별 정렬 코드 봐주세요~
fetch(fourthUrl, options)
.then(async res => await res.json())
.then(async res => {
obj_payouts = await res.data;
const sortedPayouts = obj_payouts.sort((a, b) => b.paidOn - a.paidOn);
for (let i = 0; i < sortedPayouts.length; i++) {
totalEth += parseFloat((sortedPayouts[i]["amount"] / 100) / 10000000000000000);
console.log(totalEth);
const url ='https://api.upbit.com/v1/candles/minutes/1?market=KRW-ETH&count=1&to=' + new Date(sortedPayouts[i]['paidOn'] * 1000).toISOString().replace(".000", "");
const newOption = {
method: 'GET',
headers: {
Accept: 'application/json'
}
}
//sleep(100);
fetch(url, newOption)
.then(res => res.json())
.then(res => {
totalPay += (res[0]["trade_price"] * parseFloat((sortedPayouts[i]["amount"] / 100) / 10000000000000000).toFixed(5));
var tr = document.createElement('tr');
tr.innerHTML = `
<td>${new Date(sortedPayouts[i]['paidOn'] * 1000).toISOString().split("T")[0] + " " + new Date(sortedPayouts[i]['paidOn'] * 1000).toISOString().split("T")[1].replace(".000Z", "")}</td>
<td style="line-height: 20px;"><span class="hash">${sortedPayouts[i]['start']}</span><a href="https://www.etherchain.org/block/${sortedPayouts[i]['start']}" style="margin-left:5px; border:1px solid #ddd; padding:0 3px; color:#bbb; float:right;" target="_blank"><i class="fa fa-angle-double-right" aria-hidden="true"></i></a></td>
<td><img src="http://golden-mining.com/app/img/upbit.png" style='width: 22px; margin-right: 5px; vertical-align: middle;'>${(Math.round((res[0]["trade_price"] * parseFloat((sortedPayouts[i]["amount"] / 100) / 10000000000000000).toFixed(5)))).toLocaleString()}원</td>
<td><img src="http://golden-mining.com/app/img/eth.png" style='width: 22px; vertical-align: middle;'>${parseFloat((sortedPayouts[i]["amount"] / 100) / 10000000000000000).toFixed(5)} ETH</td>
<td><span class="hash">${String(sortedPayouts[i]['txHash']).slice(0, 10)}</span> <a href="https://www.etherchain.org/tx/${sortedPayouts[i]['txHash']}" style="margin-left:5px; border:1px solid #ddd; padding:0 3px; color:#bbb; float:right;" target="_blank"><i class="fa fa-angle-double-right" aria-hidden="true"></i></a></td>
`;
table.appendChild(tr);
});
}
답변 1
결국은 paidOn의 비교가 제대로 되는지가 문제가 될 것 같습니다.
넘어오는 값이 마이너스로 비교가 되는지 > 를 사용해야 되는지...
다음을 참조해서 비교가 잘되는지 프린트해보세요.
https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value
답변을 작성하시기 전에 로그인 해주세요.