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: The person who will be the acquired company, the mobile number exists in the original company, carries on the combined wages and deletes 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: Multiply the remaining staff wages of the acquired company by 1.2 and change 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: Defines a new array of objects, connecting the two arrays var aftereat=wechatusers.concat (baiduusers)//step4: Calculates the maximum wage after the acquisition, the minimum wage, the average wage, the average wage of male, Female's average salary, etc.
//
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 ("Gender unrecognized Pro" );}} var avgmalevar 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=0var summarysalary=0var bottomsalary=99999999999f or (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.lengthconsole.log (bottomsalary); Console.log (topsalary); Console.log (summarysalary); Console.log (Avgall ); ConsoLe.log (avg (arrsalary));//-------Full employee average wage console.log (avg (malesalary));//-------Male employee Average salary console.log (avg ( femalesalary);//-------Female employee Average salary//STEP5: Find out the employee's name and phone number after the acquisition is higher than 8000, by salary from high to low sort function sortby (filed) {return function (a , b) {return B[filed]-a[filed]}}aftereat.sort (SortBy ("salary")) var arrmorethan8000 = new Array (); var userarray = Functio N (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: Find out the employee's name and phone number with the highest wage gain before and after the acquisition, and the percentage of gains 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: Find out the three names with the most names after the acquisition, unifying their average age var tempobj = {};var lll=aftereatfor (var i=lll.length-1; i>=0; i--) {if (tempobj[ Lll[i].name]) {//Get the number of occurrences of the element and make up obj tempobj[lll[i].name] = tempobj[lll[i].name]+1; }else{Tempobj[lll[i].name] = 1; }} var mostval;for (var key in tempobj) {//Gets the most occurrences of the element in the array from the object 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)
Object array job upgrade in JavaScript