JavaScript中对象数组 作业升级版
Posted 夏天里的Jasmine
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript中对象数组 作业升级版相关的知识,希望对你有一定的参考价值。
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,并且改变ID var 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+cirrent;})
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 avgmale var avgfemale function 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=0 var summarysalary=0 var bottomsalary=99999999999 for(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) ) } } arrmorethan8000 var 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=Aftereat for(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.mostVal var name=[] for (var i = 0; i < Aftereat.length; i++) { if(Aftereat[i].name==mostVal) {name.push(Aftereat[i].age)} } avg(name)
以上是关于JavaScript中对象数组 作业升级版的主要内容,如果未能解决你的问题,请参考以下文章
HTML5期末大作业:餐饮美食网站设计——咖啡(10页) HTML+CSS+JavaScript 学生DW网页设计作业成品 web课程设计网页规划与设计 咖啡网页设计 美食餐饮网页设计...(代码片段
VSCode自定义代码片段12——JavaScript的Promise对象