Object Array Job in JavaScript

  1 var BaiduUsers = [], WechatUsers = [];
  2 var User = function(id, name, phone, gender, age, salary) {
  3 this.id = id;
  4 this.name = name;
  5 this.phone = phone;
  6 this.gender = gender;
  7 this.age = age;
  8 this.salary = salary;
  9 };
 10 User.create = function(id, name, phone, gender, age, salary) {
 11     return new User(id, name, phone, gender, age, salary);
 12 };
 13 BaiduUsers.push(User.create(1, 'tommy', '1111','male', 18, 10000));
 14 BaiduUsers.push(User.create(2, 'jerry', '2222','male', 28, 10000));
 15 BaiduUsers.push(User.create(3, 'raobin','3333','female', 14, 1200));
 16 BaiduUsers.push(User.create(4, 'binbin','4444','male', 23, 9800));
 17 BaiduUsers.push(User.create(5, 'arthur','5555','female', 22, 10000));
 18 WechatUsers.push(User.create(1, 'tommy', '1111','male',20, 40000));
 19WechatUsers.push(User.create(2, 'allen', '6666','male', 34, 15800 ));
 20 WechatUsers.push(User.create(3, 'raobin','3333','female' ,16, 2300 ));
 21 WechatUsers.push(User.create(4, 'harvey','7777','male',30, 29800 ));
 22 WechatUsers.push(User.create(5, 'yuyu' , '8888','female',27, 7000 ));
 23  
24  // Step1: If the mobile phone number of the acquired company exists in the original company, merge the salary and delete 
25  for ( var i = 0; i < BaiduUsers.length; i++ ) {
 26  for ( var y = 0; y< WechatUsers.length;y++) {
 27 if (BaiduUsers[i].phone==WechatUsers[y].phone)
 28  {
 29 WechatUsers[y].oldsalary= WechatUsers[y].salary;
 30 WechatUsers[y].salary=WechatUsers[y].salary+ BaiduUsers[i].salary;
 31 BaiduUsers.splice( i,1 )
 32  }
 33  }
 34  }
 35  // Step2: Multiply the salary of the remaining employees of the acquired company by 1.2, and change the ID 
36  var len= WechatUsers.length;
 37  for ( var i = 0; i < BaiduUsers.length ; i++ ) {
 38 BaiduUsers[i].oldsalary = BaiduUsers[i].salary;
 39BaiduUsers[i].salary = BaiduUsers[i].salary*1.2 ;
 40 BaiduUsers[i].id = WechatUsers.length+1+ i;
 41  }
 42  // Step3: Define a new object array and connect the above two arrays 
43  var Aftereat= WechatUsers.concat(BaiduUsers)
 44  // Step4: Calculate the maximum salary after acquisition, minimum salary, average salary, male average salary, female average salary, etc. 
45  var arrsalary = []
 46  for ( var i= 0;i<Aftereat.length;i++ ){
 47  arrsalary.push(Aftereat[i].salary)
 48  }
 49  
50  
51  var malesalary = []
 52 var femalesalary =[]
 53 for(var i=0;i<Aftereat.length;i++){
 54 if(Aftereat[i].gender=='male'){
 55 malesalary.push(Aftereat[i].salary)
 56 }else if (Aftereat[i].gender=='female'){
 57 femalesalary.push(Aftereat[i].salary)
 58 }else{console.log("性别无法识别亲");
 59 }
 60 }
 61 
 62 
 63 var avgmale
 64 var avgfemale
 65 
 66 function avg(dataIntArr) {
 67         var avg=0;
 68         for(var i=0;i<dataIntArr.length;i++){
 69 
 70             avg+=parseFloat(dataIntArr[i]);
 71         }
 72         return avg/dataIntArr.length;
 73     }
 74     
 75     
 76 avg(arrsalary)
 77 avg(malesalary)
 78 avg(femalesalary)
 79 
 80 
 81 var topsalary=0
 82 var summarysalary=0
 83 var bottomsalary=99999999999
 84 for(var i=0;i<arrsalary.length;i++){
 85 summarysalary +=arrsalary[i];
 86 if(arrsalary[i]>topsalary)
 87 {topsalary=arrsalary[i]}
 88 if(arrsalary[i]<bottomsalary)
 89 {bottomsalary=arrsalary[i]}
 90 }
 91 
 92 var avgall=summarysalary/arrsalary.length
 93 console.log(bottomsalary);
 94 console.log(topsalary);
 95 console.log(summarysalary);
 96 console.log(avgall);
 97 console.log(avg(arrsalary)); // ------- Average salary of all employees 
98 console.log(avg(malesalary)); // ------ -The average salary of male employees is 
99 console.log(avg(femalesalary)); // -------The average salary of female employees is 
100  
101  
102  // Step5: Find out the names and phone numbers of employees whose salary is higher than 8000 after the acquisition , sorted by salary from high to low 
103  function sortBy(filed)
 104  {
 105  return  function (a,b){
 106  return b[filed]- a[filed]
 107  }
 108  }
 109 Aftereat.sort(sortBy("salary" ))
 110 
111 
112 
113 var arrmorethan8000 = new Array();
114 var UserArray = function(name, phone) {
115  this.name = name;
116  this.phone = phone;
117  };
118 User.add = function(name, phone) {
119  return new UserArray(name, phone);
120 }; 
121 
122 for (var i = 0; i < Aftereat.length; i++) {
123 if (Aftereat[i].salary>8000){
 124  arrmorethan8000.push(
 125  User.add(Aftereat[i].name,Aftereat[i].phone)
 126  )
 127  }
 128  }
 129  arrmorethan8000
 130  
131  // Step6: Find the name of the employee with the highest salary increase before and after the acquisition and phone number, and percentage of increase 
132  for ( var i = 0; i < Aftereat.length; i++ ) {
 133 Aftereat[i].salaryrise=Aftereat[i].saraly - Aftereat[i].oldsalary ;
 134  console. log(Aftereat[i].oldsalary)
 135 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324930551&siteId=291194637