金狮镖局 Design By www.egabc.com
本文实例讲述了JS数组的高级使用方法。分享给大家供大家参考,具体如下:
//数组的高级使用 var array = [10,12,20,30]; for(var index in array){ console.log(array[index]); } //length 数组长度 for(var i = 0; i < array.length; i++){ console.log(array[i]); } //数组添加新数据 array.push(1000); array.push(2000); array.push("hello world"); array.push({key:"jadeshu"}); console.log(array); //[10, 12, 20, 30, 1000, 2000, "hello world", {key:"jadeshu"}] //数组删除最后一个数据 array.pop(); console.log(array); // [10, 12, 20, 30, 1000, 2000, "hello world"] //查找数组里面值的索引 var idex = array.indexOf(2000); console.log(idex); //5 //数组删除 //splice(开始索引,索引之后的个数) var data = array.splice(2,3); console.log(data); //[20, 30, 1000] console.log(array); //[10, 12, 2000, "hello world"]
1.给定一个数组,让元素按照从大到小,从小到大排序
var array_num = [12,12,13,564,7,55,66]; //从小到大排序 array_num.sort(function (lhs,rhs) { if (lhs < rhs){ return -1; }else if(lhs > rhs) { return 1; }else { return 0; } }) console.log(array_num) // [7, 12, 12, 13, 55, 66, 564] console.log("======================="); array_num = [12,12,13,564,7,55,66]; //从大到小排序 array_num.sort(function (lhs,rhs) { if (lhs < rhs){ return 1; }else if(lhs > rhs) { return -1; }else { return 0; } }); console.log(array_num) //[564, 66, 55, 13, 12, 12, 7] console.log("=======================");
2.随机打乱一个数组
array_num = [12,12,13,564,7,55,66]; array_num.sort(function () { if ( Math.random() < 0.5){ return -1; }else { return 1; } }); console.log(array_num); //[12, 12, 564, 13, 7, 66, 55] 随机 console.log("=======================");
3.编写程序 随机的生存[10,100)范围内的整数
function random_int_num(start,end) { return Math.floor(start + (end - start) * Math.random()); } console.log(random_int_num(10,100)); //69 随机
感兴趣的朋友可以使用在线HTML/CSS/JavaScript代码运行工具:http://tools.jb51.net/code/HtmlJsRun测试上述代码运行效果。
更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript数组操作技巧总结》、《JavaScript遍历算法与技巧总结》、《javascript面向对象入门教程》、《JavaScript数学运算用法总结》、《JavaScript数据结构与算法技巧总结》及《JavaScript错误与调试技巧总结》
希望本文所述对大家JavaScript程序设计有所帮助。
标签:
JS,数组,高级使用方法
金狮镖局 Design By www.egabc.com
金狮镖局
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
金狮镖局 Design By www.egabc.com
暂无JS数组的高级使用方法示例小结的评论...
更新日志
2024年09月25日
2024年09月25日
- 《叶倩文 真心真意过一生》[WAV+CUE][400MB]
- 潘越云.1985-世间女子(滚石25周年经典复刻版)【滚石】【WAV+CUE】
- 陈柏宇.2009-CANT.BE.HALF【SONY】【WAV+CUE】
- 李明依.1989-小女生【滚石】【WAV+CUE】
- 《李荣浩 新时代创作新人王》[WAV分轨][310MB]
- 《黑鸭子与马头琴 史上最动听草原音乐》[WAV+CUE][320MB]
- 《汽车发烧音乐精选CD18》[FLAC+CUE][7.1G]
- 郑希怡.2004-Perfect.Date【英皇娱乐】【FLAC分轨】
- 群星.1994-宝丽金超级孖宝2CD【宝丽金】【WAV+CUE】
- 林忆莲.2019-0(2023精装版)【环球】【WAV+CUE】
- Supper.Moment.2016-The.Moment【寰亚】【FLAC分轨】
- 范莉丝.1979-送郎一朵牵牛花(LP版)【大联机构】【WAV+CUE】
- 刘宇.2024-宇宙诗人【哔哟哔哟文化】【FLAC分轨】
- 《全景声白噪音》沉浸式大自然助眠 带你听自然的声音[mp3]
- 纯音入心系列纯音乐《古筝精选 十指弹一赏心曲一心奏出世人心》1CD[MP3][582MB]