博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JavaScript中对象数组 作业题目以及作业
阅读量:6614 次
发布时间:2019-06-24

本文共 6874 字,大约阅读时间需要 22 分钟。

var BaiduUsers = [],WechatUsers = [];var User = function(id, name, phone, gender, age, salary) {    this.id = id;    this.name = name;    this.phone = phone;    this.gender = gender;    this.age = age;    this.salary = salary;};User.create = function(id, name, phone, gender, age, salary) {    return new User(id, name, phone, gender, age, salary);};BaiduUsers.push(User.create(1, 'tommy', '1111', 'male', 18, 10000));BaiduUsers.push(User.create(2, 'jerry', '2222', 'male', 28, 10000));BaiduUsers.push(User.create(3, 'raobin', '3333', 'female', 14, 1200));BaiduUsers.push(User.create(4, 'binbin', '4444', 'male', 23, 9800));BaiduUsers.push(User.create(5, 'yuyu', '5555', 'female', 22, 10000));WechatUsers.push(User.create(1, 'yuyu', '1111', 'male', 20, 40000));WechatUsers.push(User.create(2, 'allen', '6666', 'male', 34, 15800));WechatUsers.push(User.create(3, 'raobin', '3333', 'female', 16, 2300));WechatUsers.push(User.create(4, 'harvey', '7777', 'male', 30, 29800));WechatUsers.push(User.create(5, 'yuyu', '8888', 'female', 27, 7000));//Step1:将被收购公司的人员,手机号存在于原公司的,进行合并工资并且删除for (var i = 0; i < BaiduUsers.length; i++) {    for (var y = 0; y < WechatUsers.length; y++) {        if (BaiduUsers[i].phone == WechatUsers[y].phone) {            WechatUsers[y].oldsalary = WechatUsers[y].salary;            WechatUsers[y].salary = WechatUsers[y].salary + BaiduUsers[i].salary;            BaiduUsers.splice(i, 1)        }    }}//Step2:将被收购公司剩余人员工资乘以1.2,并且改变IDvar len = WechatUsers.length;for (var i = 0; i < BaiduUsers.length; i++) {    BaiduUsers[i].oldsalary = BaiduUsers[i].salary;    BaiduUsers[i].salary = BaiduUsers[i].salary * 1.2;    BaiduUsers[i].id = WechatUsers.length + 1 + i;}//Step3:定义一个新对象数组,连接上述两个数组var Aftereat = WechatUsers.concat(BaiduUsers)//Step4:计算收购之后的最高工资,最低工资,平均工资,male的平均工资,female的平均工资等//全公司的平均工资var avgall = Aftereat.map(function(value) {    return value.salary;}).reduce(function(previous, current, index, array) {    return previous + current;}) / Aftereat.length;//男员工的平均工资Aftereat.filter(function(value) {    return value.gender == 'male'}).map(function(value) {    return value.salary;}).reduce(function(previous, current, index, array) {    return previous + current;}) / Aftereat.filter(function(value) {    return value.gender == 'male'}).length;//女员工的平均工资Aftereat.filter(function(value) {    return value.gender == 'female'}).map(function(value) {    return value.salary;}).reduce(function(previous, current, index, array) {    return previous + current;}) / Aftereat.filter(function(value) {    return value.gender == 'female'}).length;//收购之后最高工资Math.max.apply(null, Aftereat.map(function(value) {    return value.salary;}))//收购之后最低工资Math.min.apply(null, Aftereat.map(function(value) {    return value.salary;}))//原始方法如下var arrsalary = []for (var i = 0; i < Aftereat.length; i++) {    arrsalary.push(Aftereat[i].salary)}var malesalary = []var femalesalary = []for (var i = 0; i < Aftereat.length; i++) {    if (Aftereat[i].gender == 'male') {        malesalary.push(Aftereat[i].salary)    } else if (Aftereat[i].gender == 'female') {        femalesalary.push(Aftereat[i].salary)    } else {        console.log("性别无法识别亲");    }}var avgmalevar avgfemalefunction avg(dataIntArr) {    var avg = 0;    for (var i = 0; i < dataIntArr.length; i++) {        avg += parseFloat(dataIntArr[i]);    }    return avg / dataIntArr.length;}avg(arrsalary) avg(malesalary) avg(femalesalary)var topsalary = 0var summarysalary = 0var bottomsalary = 99999999999for (var i = 0; i < arrsalary.length; i++) {    summarysalary += arrsalary[i];    if (arrsalary[i] > topsalary) {        topsalary = arrsalary[i]    }    if (arrsalary[i] < bottomsalary) {        bottomsalary = arrsalary[i]    }}var avgall = summarysalary / arrsalary.length console.log(bottomsalary);console.log(topsalary);console.log(summarysalary);console.log(avgall);console.log(avg(arrsalary)); //-------全员工平均工资console.log(avg(malesalary)); //-------男员工平均工资console.log(avg(femalesalary)); //-------女员工平均工资//Step5:找出收购后工资高于8000的员工姓名和电话号码,按薪水从高到低排序function sortBy(filed) {    return function(a, b) {        return b[filed] - a[filed]    }}Aftereat.sort(sortBy("salary"))var arrmorethan8000 = new Array();var UserArray = function(name, phone) {    this.name = name;    this.phone = phone;};UserArray.add = function(name, phone) {    return new UserArray(name, phone);};for (var i = 0; i < Aftereat.length; i++) {    if (Aftereat[i].salary > 8000) {        arrmorethan8000.push(UserArray.add(Aftereat[i].name, Aftereat[i].phone))    }}arrmorethan8000var MMM = Aftereat//Step6:找出收购前后工资涨幅最高的员工姓名和电话号码,以及涨幅的百分比for (var i = 0; i < MMM.length; i++) {    if (MMM[i].oldsalary != undefined) {        MMM[i].salaryrise = MMM[i].salary - MMM[i].oldsalary;        MMM[i].percentrise = MMM[i].salaryrise / MMM[i].oldsalary * 100;    } else {        MMM.splice(i, 1)    }}function sortBy(filed) {    return function(a, b) {        return b[filed] - a[filed]    }}MMM.sort(sortBy("percentrise"))//Step7:找出收购后重名最多的三个姓名,统一出他们的平均年龄var tempObj = {};var LLL = Aftereatfor (var i = LLL.length - 1; i >= 0; i--) {    if (tempObj[LLL[i].name]) { // 得到元素出现的次数,并组成obj        tempObj[LLL[i].name] = tempObj[LLL[i].name] + 1;    } else {        tempObj[LLL[i].name] = 1;    }}var mostVal;for (var key in tempObj) { // 从对象中得到数组中出现最多的元素    if (!mostVal) {        mostVal = key;    } else if (tempObj[mostVal] < tempObj[key]) {        mostVal = key;    }}delete tempObj.mostValvar name = []for (var i = 0; i < Aftereat.length; i++) {    if (Aftereat[i].name == mostVal) {        name.push(Aftereat[i].age)    }}avg(name)  

  作业要求

var BaiduUsers = [], WechatUsers = [];

var User = function(id, name, phone, gender, age, salary) {
this.id = id;
this.name = name;
this.phone = phone;
this.gender = gender;
this.age = age;
this.salary = salary;
};
User.create = function(id, name, phone, gender, age, salary) {
return new User(id, name, phone, gender, age, salary);
};
BaiduUsers.push(User.create(1, 'tommy', '1111','male', 18, 10000));
BaiduUsers.push(User.create(2, 'jerry', '2222','male', 28, 10000));
BaiduUsers.push(User.create(3, 'raobin','3333','female', 14, 1200));
BaiduUsers.push(User.create(4, 'binbin','4444','male', 23, 9800));
BaiduUsers.push(User.create(5, 'arthur','5555','female', 22, 10000));
WechatUsers.push(User.create(1, 'tommy', '1111','male', 20, 40000));
WechatUsers.push(User.create(2, 'allen', '6666','male', 34, 15800));
WechatUsers.push(User.create(3, 'raobin','3333','female',16, 2300));
WechatUsers.push(User.create(4, 'harvey','7777','male',30, 29800));
WechatUsers.push(User.create(5, 'yuyu', '8888','female',27, 7000));

Wechat收购Baidu,电话号码相同的人当作是同一个人,合并后salary相加,其他属性保留Wechat的数据,新的Baidu的员工重新生成id,salary涨幅20%
统计收购后的员工平均工资,最高工资,最低工资,male的平均工资,female的平均工资
找出收购后工资高于8000的员工姓名和电话号码,按薪水从高到低排序
找出收购前后工资涨幅最高的员工姓名和电话号码,以及涨幅的百分比
找出收购后重名最多的三个姓名,统一出他们的平均年龄

转载于:https://www.cnblogs.com/qianjinyan/p/8951916.html

你可能感兴趣的文章
种子填充算法描述及C++代码实现
查看>>
Kali渗透测试——快速查找Metasploit的模块
查看>>
如何生成项目的chm文档
查看>>
java封装httpClient工具(支持http和https,包含get和post请求)
查看>>
Rocket - diplomacy - LazyModuleImpLike
查看>>
如何取消OneNote的粘贴来源地址
查看>>
Nginx+Tomcat实现动静分离
查看>>
Exchange Server 2016管理系列课件25.管理安全通讯组
查看>>
考前必背的50个知识点——系统集成项目管理工程师考试
查看>>
计算机科学,大一学生怎样来爱你(文&PPT)
查看>>
linux中vmstat命令详解
查看>>
PHP 开发社区微信服务号实战图解
查看>>
Exchange Server 2013 系列八:邮箱服务器角色DAG实战
查看>>
php使用curl下载指定大小的文件
查看>>
VS2013创建Node.js C++ Addons的过程
查看>>
amaze ui中的icon button
查看>>
tcp 三次握手
查看>>
XML中添加换行符
查看>>
JVM内存配置
查看>>
实战-JavaWweb的Servlet和Filter运行关系(四)
查看>>