테스트 사이트 - 개발 중인 베타 버전입니다

json 날짜별로 출력 채택완료

그누스트랩 4년 전 조회 2,724

API를 통해 리스트를 뿌려주는데 날짜별로 정렬하고자합니다 새로고침때마다 랜덤으로 출력되고 있습니다

날짜별 정렬 코드 봐주세요~

</strong></p>

<p>                        fetch(fourthUrl, options)

                            .then(async res => await res.json())

                            .then(async res => {

                                obj_payouts = await res.data;</p>

<p>                                const sortedPayouts = obj_payouts.sort((a, b) => b.paidOn - a.paidOn);</p>

<p>                                for (let i = 0; i < sortedPayouts.length; i++) {

                                    totalEth += parseFloat((sortedPayouts[i]["amount"] / 100) / 10000000000000000);

                                    console.log(totalEth);</p>

<p>                                    const url ='<a href="https://api.upbit.com/v1/candles/minutes/1?market=KRW-ETH&count=1&to='" target="_blank" rel="noopener noreferrer">https://api.upbit.com/v1/candles/minutes/1?market=KRW-ETH&count=1&to='</a> + 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));</p>

<p>                                        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="<a href="https://www.etherchain.org/block/${sortedPayouts[i]['start']}"" target="_blank" rel="noopener noreferrer">https://www.etherchain.org/block/${sortedPayouts[i]['start']}"</a> 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="<a href="http://golden-mining.com/app/img/upbit.png"" target="_blank" rel="noopener noreferrer">http://golden-mining.com/app/img/upbit.png"</a> 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="<a href="http://golden-mining.com/app/img/eth.png"" target="_blank" rel="noopener noreferrer">http://golden-mining.com/app/img/eth.png"</a> 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="<a href="https://www.etherchain.org/tx/${sortedPayouts[i]['txHash']}"" target="_blank" rel="noopener noreferrer">https://www.etherchain.org/tx/${sortedPayouts[i]['txHash']}"</a> 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);

                                    });</p>

<p>                                }</p>

<p><strong>
 

댓글을 작성하려면 로그인이 필요합니다.

답변 1개

채택된 답변
+20 포인트
4년 전

결국은 paidOn의 비교가 제대로 되는지가 문제가 될 것 같습니다.

 

넘어오는 값이 마이너스로 비교가 되는지 > 를 사용해야 되는지...

 

다음을 참조해서 비교가 잘되는지 프린트해보세요.

 

https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인