美文网首页
数组的截取合并和数组对象的排序

数组的截取合并和数组对象的排序

作者: 小羊子简述 | 来源:发表于2019-06-28 10:40 被阅读0次

    $(function(){

    $.get("http://localhost:3000/number",function(data){

    console.log(data)

    var data=data

    var a=data.slice(0,4)//截取数组的前4项

    b=data.slice(-4)//截取数组的后4项

    var c = a.concat(b) //合并2个截取数组生产一个新数组

    c.sort(compare('AMPLITUDE')); //调用函数给新数组以AMPLITUDE排序

    console.log(c)

    })

    })

    var student = [{"name":"lisi","age":18,"sex":"male"},

    {"name":"zhangsan","age":17,"sex":"female"},

    {"name":"wangwu","age":21,"sex":"shemale"}];

    student.sort(compare("age"));

    console.dir(student);

    function compare(protoTypeName) {

    return function(object1,object2){

    var val1 = object1[protoTypeName];

    var val2 = object2[protoTypeName];

    if(val1<val2){

    return -1;

    }else if(val1>val2){

    return 1;

    }else{

    return 0;

    }

    }

    }

    </script>

    相关文章

      网友评论

          本文标题:数组的截取合并和数组对象的排序

          本文链接:https://www.haomeiwen.com/subject/cgfscctx.html