數組里添加一行數據(splice)


<script>
        var bigShotList = [
            { payAmount: 80000, tel: "158****6871", isMy: "2" },
            { payAmount: 10000, tel: "136****5432", isMy: "2" },
            { payAmount: 4791.37, tel: "136****1118", isMy: "2" },
            { payAmount: 2000, tel: "136****0096", isMy: "2" },
            { payAmount: 1500, tel: "136****0118", isMy: "2" },
            { payAmount: 1000, tel: "136****4357", isMy: "2" },
            { payAmount: 800, tel: "136****4350", isMy: "2" },
            { payAmount: 500, tel: "136****1144", isMy: "2" },
            { payAmount: 500, tel: "136****1119", isMy: "2" },
            { payAmount: 200, tel: "136****4327", isMy: "2" }
        ]
        var addlist = { payAmount: 500, tel: "000****0000", isMy: "添加的數據" }
        var row = "";
        for (var i = 0; i < bigShotList.length; i++) {
            if (addlist.payAmount >= bigShotList[i].payAmount) {
                row = i;
                break
            } else {
                row = bigShotList.length;
            }
        }
        console.log( '插入位置' +row)
        bigShotList.splice(row, 0, addlist)
        console.log(bigShotList)
結果:------------
        

 

 

        
    </script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM