练习:对数组进行排序需求:对两个数组进行从小到大排序后输出相对应的数值。两个数组var movieArr =隐秘而伟大八佰疯狂动物城头师傅一体; var boxofficeArr = 14733912153;将电影名票房插入网页表格javasciver实现
var movieArr =['隐秘而伟大','八佰','疯狂动物城','头师傅一体']; var boxofficeArr = [14.7,33.9,12,15.3];
// 对boxofficeArr数组进行排序 boxofficeArr.sort(function(a, b){ return a - b; });
// 创建表格 var table = document.createElement('table'); var tbody = document.createElement('tbody');
// 循环遍历电影数组 for (var i = 0; i < movieArr.length; i++) { var movie = movieArr[i]; var boxoffice = boxofficeArr[i];
// 创建表格行
var row = document.createElement('tr');
// 创建电影单元格
var movieCell = document.createElement('td');
movieCell.textContent = movie;
// 创建票房单元格
var boxofficeCell = document.createElement('td');
boxofficeCell.textContent = boxoffice;
// 将单元格添加到行
row.appendChild(movieCell);
row.appendChild(boxofficeCell);
// 将行添加到表格主体
tbody.appendChild(row);
}
// 将表格主体添加到表格 table.appendChild(tbody);
// 将表格添加到网页 document.body.appendChild(table)
原文地址: https://www.cveoy.top/t/topic/iHDU 著作权归作者所有。请勿转载和采集!