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 + 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 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)
作业要求
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的员工姓名和电话号码,按薪水从高到低排序
找出收购前后工资涨幅最高的员工姓名和电话号码,以及涨幅的百分比
找出收购后重名最多的三个姓名,统一出他们的平均年龄
JavaScript中对象数组 作业题目以及作业的更多相关文章
- JavaScript中对象数组 作业
var BaiduUsers = [], WechatUsers = []; var User = function(id, name, phone, gender, age, salary) { t ...
- JavaScript中对象数组 根据某个属性值 然后push到新的数组
原文链接 https://segmentfault.com/q/1010000010075035 将下列对象数组中,工资大于1w的员工,增加到对象数组 WanSalary中 var BaiduUser ...
- JavaScript中对象数组去重方法
在一次对后端返回的对象数组的操作时想通过indexOf()或者includes()的方法来实现对对象数组的去重但是行不通,因为用indexOf()返回的都是-1,一下记录两种对象数组(更具指定属性)去 ...
- JavaScript中对象数组 作业 题目如下
var BaiduUsers = [], WechatUsers = []; var User = function(id, name, phone, gender, age, salary) { t ...
- JavaScript中对象数组,如何给对象添加一个新属性
var a =[{name: 'Tom',age:20},{name: 'Tom2',age:22}] 现在给a数组中的第一个对象添加性别属性 a[0]['gender']='women' a[0][ ...
- JavaScript中对象数组 根据某个属性值进行排序
将下列对象数组,通过工资属性,由高到低排序 var BaiduUsers = [], WechatUsers = []; var User = function(id, name, phone, ge ...
- javascript中对象和数组的异同点
一.JS声明对象或数组 JS对象:{ } JS数组:[ ] 对象 var b={m:'123',n:'abc'};alert(b.m);alert(b.n); 一维数组 var a=[1,2,3];a ...
- JavaScript中Array(数组) 对象
JavaScript中Array 对象 JavaScript中创建数组有两种方式 (一)使用直接量表示法: var arr4 = []; //创建一个空数组var arr5 = [20]; // 创建 ...
- javascript中对象的深度克隆
记录一个常见的面试题,javascript中对象的深度克隆,转载自:http://www.2cto.com/kf/201409/332955.html 今天就聊一下一个常见的笔试.面试题,js中对象的 ...
随机推荐
- unittest之suite测试集(测试套件)
suite 这个表示测试集,不要放在class内,否则会提示"没有这样的测试方法在类里面 ",我觉得它唯一的好处就是调试的时候可以单独调试某个class而已,我一般不用它,调试时可 ...
- Sublime Text3(mac)一些插件和快捷键
Sublime Text3(mac)一些插件和快捷键 楚简约 关注 2017.02.24 17:02* 字数 1216 阅读 412评论 0喜欢 2 下载地址http://www.sublimetex ...
- MFC用串行化实现文档存储和读取功能
在面向对象的程序设计中,一般都是用二进制文件来保存文档资料.在VC++中控制和使用文件流的方法很多,MFC程序设计中常用的有两种方法:用CFile对象存储和读取文件:利用串行化存取文件.其中用CFil ...
- 怎么查看是否安装Scrapy
1.在python shell 下输入 import scrapy
- 基于Axis1.4的webservice接口开发(代码开发)
基于Axis1.4的webservice接口开发(代码开发) 一.开发环境: 我的开发环境是MyEclipse 2015+Apache-Tomcat-8.0.21. 二.代码开发: 1.新建一个Web ...
- Vert.x
Vert.x是一个基于JVM.轻量级.高性能的应用平台,非常适用于最新的移动端后台.互联网.企业应用架构.Vert.x基于全异步Java服务器Netty,并扩展出了很多有用的特性. 同时支持多种编程语 ...
- AtCoder Beginner Contest 086 D - Checker
Time limit : 2sec / Memory limit : 256MB Score : 500 points Problem Statement AtCoDeer is thinking o ...
- bzoj3524 [Poi2014]Couriers/2223 [Coci 2009]PATULJCI
题目链接1 题目链接2 主席树模板题 两题有细节不同 #include<algorithm> #include<iostream> #include<cstdlib> ...
- C++ 电路布线/最短路径问题
问题描述 用二维数组表示地图,若值为 1 则表示有障碍物,若值为 0 则表示可以通行. 输入: m*n 的二维数组,布线起点坐标,布线终点坐标. 输出: 最短布线距离以及对应的布线路径. 问题分析 从 ...
- bzoj3505 / P3166 [CQOI2014]数三角形
P3166 [CQOI2014]数三角形 前置知识:某两个点$(x_{1},,y_{1}),(x_{2},y_{2})\quad (x_{1}<x_{2},y_{1}<y_{2})$所连成 ...