运行 ❯
获取您的
自有
网站
×
更改方向
保存代码
更改主题,暗色/亮色
前往 Spaces
<!DOCTYPE html> <html> <body> <h1>JavaScript Arrays</h1> <h2>The sort() Method</h2> <p>Sort car objects on age:</p> <p id="demo"></p> <script> const cars = [ {type:"Volvo", year:2016}, {type:"Saab", year:2001}, {type:"BMW", year:2010} ]; displayCars(); cars.sort(function(a, b){return a.year - b.year}); displayCars(); function displayCars() { document.getElementById("demo").innerHTML = cars[0].type + " " + cars[0].year + "<br>" + cars[1].type + " " + cars[1].year + "<br>" + cars[2].type + " " + cars[2].year; } </script> </body> </html>