JavaScript中对象数组作业题⽬以及作业var BaiduUsers = [],
WechatUsers = [];
var User = function(id, name, phone, gender, age, salary) {
this.id = id;
this.name = name;
this.phone = phone;
this.age = age;
this.salary = salary;
};
return new User(id, name, phone, gender, age, salary);
};
BaiduUsers.ate(1, 'tommy', '1111', 'male', 18, 10000));
BaiduUsers.ate(2, 'jerry', '2222', 'male', 28, 10000));
BaiduUsers.ate(3, 'raobin', '3333', 'female', 14, 1200));
BaiduUsers.ate(4, 'binbin', '4444', 'male', 23, 9800));
BaiduUsers.ate(5, 'yuyu', '5555', 'female', 22, 10000));
WechatUsers.ate(1, 'yuyu', '1111', 'male', 20, 40000));
WechatUsers.ate(2, 'allen', '6666', 'male', 34, 15800));
WechatUsers.ate(3, 'raobin', '3333', 'female', 16, 2300));
WechatUsers.ate(4, 'harvey', '7777', 'male', 30, 29800));
WechatUsers.ate(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 = at(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) {
der == 'male'
}).map(function(value) {
return value.salary;
}).reduce(function(previous, current, index, array) {
return previous + current;
}) / Aftereat.filter(function(value) {
der == 'male'
}).length;
//⼥员⼯的平均⼯资
Aftereat.filter(function(value) {
der == 'female'
}).map(function(value) {
return value.salary;
}).reduce(function(previous, current, index, array) {
return previous + current;
}) / Aftereat.filter(function(value) {
der == '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 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]
filter过滤对象数组
}
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;
}
}
stVal
var 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.age = age;
this.salary = salary;
};
return new User(id, name, phone, gender, age, salary);
};
BaiduUsers.ate(1, 'tommy', '1111','male', 18, 10000));
BaiduUsers.ate(2, 'jerry', '2222','male', 28, 10000));
BaiduUsers.ate(3, 'raobin','3333','female', 14, 1200));
BaiduUsers.ate(4, 'binbin','4444','male', 23, 9800));
BaiduUsers.ate(5, 'arthur','5555','female', 22, 10000));
WechatUsers.ate(1, 'tommy', '1111','male', 20, 40000));
WechatUsers.ate(2, 'allen', '6666','male', 34, 15800));
WechatUsers.ate(3, 'raobin','3333','female',16, 2300));
WechatUsers.ate(4, 'harvey','7777','male',30, 29800));
WechatUsers.ate(5, 'yuyu', '8888','female',27, 7000));
Wechat收购Baidu,电话号码相同的⼈当作是同⼀个⼈,合并后salary相加,其他属性保留Wechat的数据,新的Baidu的员⼯重新⽣成id,salary涨幅20%
统计收购后的员⼯平均⼯资,最⾼⼯资,最低⼯资,male的平均⼯资,female的平均⼯资
出收购后⼯资⾼于8000的员⼯姓名和电话号码,按薪⽔从⾼到低排序
出收购前后⼯资涨幅最⾼的员⼯姓名和电话号码,以及涨幅的百分⽐
出收购后重名最多的三个姓名,统⼀出他们的平均年龄

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。