七 数组的高级使用

//数组的高级使用

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 随机

 

转载于:https://www.cnblogs.com/jadeshu/p/10663671.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值