继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

JavaScript 数组练习题之实现

前端GoGoGo
关注TA
已关注
手记 59
粉丝 116
获赞 6940

图片描述
JavaScript 数组练习题见这里

题 1:查找数组对象中 age 大于 18 对象

function filterAdult (arr) {
    var res = [];
    if(Array.isArray(arr)){
        if(arr.filter){// 如果浏览器支持filter
            res = arr.filter(function (each) {
                return each.age > 18;
            })
        } else {
            arr.forEach(function  (each) {
                if(each.age > 18){
                    res.push(each);
                }
            });
        }
    }
    return res;
}

题 2:判断数组中是否所有的数字都大于 0

function isAllNumPosive (arr) {
    var res = true;
    if(Array.isArray(arr)){
        if(arr.every){
            res = arr.every(function (each) {
                var isPosive = true;
                if(typeof each === 'number' && each < 0){
                    isPosive = false;
                }
                return isPosive;
            });
        } else {
            arr.forEach(function  (each) {
                if(typeof each === 'number' && each < 0){
                    res = false;
                }
            });
        }
    } else {
        throw new TypeError('param is not Array');
    }
    return res;

}

题 3:改变传入的数组,将数组中第 n(从 0 开始算 ) 个元素放到数组的开头

function putFirst (arr, n) {
    if(Array.isArray(arr)){
        var item = arr.splice(n, 1)[0];
        arr.unshift(item);
    }
}

题 4: 将 arguments 对象转换成数组

function toArray (arrLikeObj) {
    return [].slice.call(arrLikeObj);
}

题 5:将数组中数字内容求和

function sum (arr) {
    var res = 0;
    if(Array.isArray(arr)){
        if(arr.reduce){
            res = arr.reduce(function (prev, curr) {
                if(typeof curr === 'number'){
                    return prev + curr;
                }
                return prev;
            }, 0)
        } else {
            arr.forEach(function  (each) {
                if(typeof each === 'number'){
                    res = res + each;
                }
            });
        }
    }
    return res;
}

题 6: 将数组元素按 age 字段的值,从小到大排序

function sortAge (arr) {
    var res = false;
    if(Array.isArray(arr)){
        res = arr.sort(function (a, b) {
            return a.age < b.age ? -1: 1;
        });
    } else {
        throw new TypeError('param is not Array');
    }
    return res;
}

题 7: 将数组元素去重,其中数组元素均为基本类性

function uniq (arr) {
    var res = [];
    if(Array.isArray(arr) ){
        arr.forEach(function  (each) {
            if(res.indexOf(each) === -1){
                res.push(each);
            }
        })
    } else {
        throw new TypeError('param is not Array');
    }
    return res;
}
// 或
function uniq (arr) {
    var res = [];
    var cache = {};
    if(Array.isArray(arr) ){
        arr.forEach(function  (each) {
            if(cache[each] === undefined){
                res.push(each);
                cache[each] = true;
            }
        })
    } else {
        throw new TypeError('param is not Array');
    }
    return res;
}

题 8: 将数组内容乱序

function random(arr) {
    var res;
    if (Array.isArray(arr)) {
        res = arr.sort(function() {
            return Math.random() > 0.5 ? 1 : -1;
        })
    } else {
        throw new TypeError('param is not Array');
    }
    return res;
}

本文遵守创作共享CC BY-NC-SA 4.0协议
网络平台如需转载必须与本人联系确认。

打开App,阅读手记
50人推荐
发表评论
随时随地看视频慕课网APP

热门评论

看不懂,后面能有个注释才好

收藏

收藏

查看全部评论