JavaScript中的数组处理
所以我有一个javascript任务,我需要处理数组中的数据。假设下面给出了一个数组:JavaScript中的数组处理,javascript,arrays,Javascript,Arrays,所以我有一个javascript任务,我需要处理数组中的数据。假设下面给出了一个数组: var data = [ /*Object 1*/ { "personnelMateriaRel": [], "videos": [], "contactor": { "id": 18320, "mobile": "13705139529", "name": "Jack",
var data = [
/*Object 1*/
{
"personnelMateriaRel": [],
"videos": [],
"contactor": {
"id": 18320,
"mobile": "13705139529",
"name": "Jack",
"otherTel2": "",
"temobile": "",
"workUnit": ""
},
"pics": [
"/file/distribution/20191112172328535_1573550701898.jpeg"
],
"distributionResult": {
"latitude": 23.095949110729155,
"longitude": 113.28544487112273,
"time": "2020-01-02 17:04:38",
"content": "Arrived",
"address": "USA"
},
"sendTime": "2020-01-02 16:01:54"
},
/*Object 2*/
{
"personnelMateriaRel": [],
"videos": [],
"contactor": {
"id": 18320,
"mobile": "13705139529",
"name": "Jack",
"otherTel2": "",
"temobile": "",
"workUnit": ""
},
"pics": [],
"distributionResult": {
"latitude": 23.095949110729155,
"longitude": 113.28544487112273,
"time": "2020-01-02 17:04:38",
"content": "Arrived",
"address": "USA"
},
"sendTime": "2020-01-02 16:01:54"
},
/*Object 3*/
{
"personnelMateriaRel": [],
"videos": [],
"contactor": {
"id": 18320,
"mobile": "13705139529",
"name": "Jack",
"otherTel2": "",
"temobile": "",
"workUnit": ""
},
"pics": [],
"distributionResult": {
"latitude": 23.09594105827961,
"longitude": 113.28548480963536,
"time": "2020-01-02 17:34:27",
"content": "Arrived",
"address": "USA"
},
"sendTime": "2020-01-02 17:08:49"
},
/*Object 4*/
{
"personnelMateriaRel": [],
"videos": [],
"contactor": {
"id": 18320,
"mobile": "13705139529",
"name": "Jack",
"otherTel2": "",
"temobile": "",
"workUnit": ""
},
"pics": [],
"distributionResult": {
"latitude": 23.09594105827961,
"longitude": 113.28548480963536,
"time": "2020-01-02 17:34:27",
"content": "Arrived",
"address": "USA"
},
"sendTime": "2020-01-02 17:08:49"
},
/*Object 5*/
{
"personnelMateriaRel": [],
"videos": [
"/file/distribution/video"
],
"contactor": {
"id": 31903,
"mobile": "13924827229",
"name": "Mike",
"otherTel2": "",
"temobile": "",
"workUnit": ""
},
"pics": [],
"distributionResult": {
"latitude": 23.093305,
"longitude": 113.290806,
"time": "2020-01-02 20:25:03",
"content": "Arrived",
"address": "Canada"
},
"sendTime": null
},
/*Object 6*/
{
"personnelMateriaRel": [],
"videos": [],
"contactor": {
"id": 31903,
"mobile": "13924827229",
"name": "Mike",
"otherTel2": "",
"temobile": "",
"workUnit": ""
},
"pics": [
"/file/distribution/123.jpeg"
],
"distributionResult": {
"latitude": 23.093305,
"longitude": 113.290806,
"time": "2020-01-02 20:25:03",
"content": "Arrived",
"address": "Canada"
},
"sendTime": null
},
/*Object 7*/
{
"personnelMateriaRel": [],
"videos": [],
"contactor": {
"id": 31903,
"mobile": "13924827229",
"name": "Mike",
"otherTel2": "",
"temobile": "",
"workUnit": ""
},
"pics": [],
"distributionResult": {
"latitude": 23.093345,
"longitude": 113.290808,
"time": "2020-01-02 20:25:18",
"content": "Arrived",
"address": "Canada"
},
"sendTime": null
},
]
此数组包含对象。因此,对于我们拥有的每个对象:
personnelMateriaRel; videos; contactor (which includes: id, mobile, name, otherTel2, temobile, workUnit); pics; distributionResult(which includes: latitude, longitude, time, content, address); sendTime
这里我有两个对象Jack(ID=18320)和Mike(ID=31903),你可以看到每个对象都会重复两次。这是因为他们有不同的“时间”。因此,任务是返回新的数组(或其他数据结构),其中包含上一次“时间”(即最大的“时间”值)的对象,并将所有“图片”和“视频”、所有已删除的对象与最大的“时间”值的对象合并。因此,对于上述示例,要存储的正确值应为:
/*Object 3*/
{
"personnelMateriaRel": [],
"videos": [],
"contactor": {
"id": 18320,
"mobile": "13705139529",
"name": "Jack",
"otherTel2": "",
"temobile": "",
"workUnit": ""
},
"pics": [
"/file/distribution/20191112172328535_1573550701898.jpeg" /*Merged*/
],
"distributionResult": {
"latitude": 23.09594105827961,
"longitude": 113.28548480963536,
"time": "2020-01-02 17:34:27",
"content": "Arrived",
"address": "USA"
},
"sendTime": "2020-01-02 17:08:49"
},
/*Object 7*/
{
"personnelMateriaRel": [],
"videos": [
"/file/distribution/video" /*Merged*/
],
"contactor": {
"id": 31903,
"mobile": "13924827229",
"name": "Mike",
"otherTel2": "",
"temobile": "",
"workUnit": ""
},
"pics": [
"/file/distribution/123.jpeg" /*Merged*/
],
"distributionResult": {
"latitude": 23.093345,
"longitude": 113.290808,
"time": "2020-01-02 20:25:18",
"content": "Arrived",
"address": "Canada"
},
"sendTime": null
}
这里是我迄今为止尝试过的,但结果仍然不正确:
function resolve(array) {
let map = {}
let keys = ["personnelMateriaRel",
"videos", "pics", "distributionResult",
"sendTime"]
array.forEach(element => {
let id = element["contactor"]["id"]
let eleTime = element["distributionResult"]["time"]
let object = map[id]
if (!object) {
object = map[id] = {
id: id,
time: eleTime,
}
} else {
let lastTime = object["time"]
if (new Date(eleTime) <= new Date(lastTime)) {
object["time"] = lastTime
}
}
for (let value of keys) {
object[value] = object[value]
? object[value].concat(element[value])
: [].concat(element[value])
}
});
return Object.keys(map).map(id => map[id])
}
函数解析(数组){
设map={}
让keys=[“personnelMateriaRel”,
“视频”、“图片”、“分发结果”,
“发送时间”]
array.forEach(元素=>{
让id=元件[“接触器”][“id”]
让eleTime=element[“distributionResult”][“time”]
let object=map[id]
如果(!对象){
对象=映射[id]={
id:id,
时间:时间,,
}
}否则{
让lastTime=对象[“时间”]
if(新日期(时间)映射[id])
}
非常感谢您的帮助
更新
因此,根据(@thingEvery)的答案,根据他的代码,我得到的结果如下:
“时间”字段是正确的,但我仍然返回所有“时间”小于最大“时间”的对象。显示的图像是第一个对象(即插孔)。因此有4个插孔,似乎所有字段(接触器、distributionResult、sendTime)都在合并。我需要合并的只是“图片”和“视频”.如果我正确理解了你的问题,你几乎就到了。我要做的就是在你的钥匙列表中添加“接触器”并修正你的时间比较
var数据=[
/*对象1*/
{
“人员材料”:[],
“视频”:[],
“接触器”:{
“id”:18320,
“手机”:“13705139529”,
“姓名”:“杰克”,
“其他2”:“其他”,
“temobile”:“,
“工作单元”:”
},
“图片”:[
“/file/distribution/20191112172328535_15735070701898.jpeg”
],
“分配结果”:{
“纬度”:23.095949110729155,
“经度”:113.2854447112273,
“时间”:“2020-01-02 17:04:38”,
“内容”:“到达”,
“地址”:“美国”
},
“发送时间”:“2020-01-02 16:01:54”
},
/*对象2*/
{
“人员材料”:[],
“视频”:[],
“接触器”:{
“id”:18320,
“手机”:“13705139529”,
“姓名”:“杰克”,
“其他2”:“其他”,
“temobile”:“,
“工作单元”:”
},
“图片”:[],
“分配结果”:{
“纬度”:23.095949110729155,
“经度”:113.2854447112273,
“时间”:“2020-01-02 17:04:38”,
“内容”:“到达”,
“地址”:“美国”
},
“发送时间”:“2020-01-02 16:01:54”
},
/*对象3*/
{
“人员材料”:[],
“视频”:[],
“接触器”:{
“id”:18320,
“手机”:“13705139529”,
“姓名”:“杰克”,
“其他2”:“其他”,
“temobile”:“,
“工作单元”:”
},
“图片”:[],
“分配结果”:{
“纬度”:23.09594105827961,
“经度”:113.2854840963536,
“时间”:“2020-01-02 17:34:27”,
“内容”:“到达”,
“地址”:“美国”
},
“发送时间”:“2020-01-02 17:08:49”
},
/*对象4*/
{
“人员材料”:[],
“视频”:[],
“接触器”:{
“id”:18320,
“手机”:“13705139529”,
“姓名”:“杰克”,
“其他2”:“其他”,
“temobile”:“,
“工作单元”:”
},
“图片”:[],
“分配结果”:{
“纬度”:23.09594105827961,
“经度”:113.2854840963536,
“时间”:“2020-01-02 17:34:27”,
“内容”:“到达”,
“地址”:“美国”
},
“发送时间”:“2020-01-02 17:08:49”
},
/*对象5*/
{
“人员材料”:[],
“视频”:[
“/文件/分发/视频”
],
“接触器”:{
“id”:31903,
“手机”:“13924827229”,
“姓名”:“迈克”,
“其他2”:“其他”,
“temobile”:“,
“工作单元”:”
},
“图片”:[],
“分配结果”:{
“纬度”:23.093305,
“经度”:113.290806,
“时间”:“2020-01-02 20:25:03”,
“内容”:“到达”,
“地址”:“加拿大”
},
“发送时间”:空
},
/*对象6*/
{
“人员材料”:[],
“视频”:[],
“接触器”:{
“id”:31903,
“手机”:“13924827229”,
“姓名”:“迈克”,
“其他2”:“其他”,
“temobile”:“,
“工作单元”:”
},
“图片”:[
“/file/distribution/123.jpeg”
],
“分配结果”:{
“纬度”:23.093305,
“经度”:113.290806,
“时间”:“2020-01-02 20:25:03”,
“内容”:“到达”,
“地址”:“加拿大”
},
“发送时间”:空
},
/*对象7*/
{
“人员材料”:[],
“视频”:[],
“接触器”:{
“id”:31903,
“手机”:“13924827229”,
“姓名”:“迈克”,
“其他2”:“其他”,
“temobile”:“,
“工作单元”:”
},
“图片”:[],
“分配结果”:{
“纬度”:23.093345,
“经度”:113.290808,
“时间”:“2020-01-02 20:25:18”,
“内容”:“到达”,
“地址”:“加拿大”
},
“发送时间”:空
},
]
函数解析(数组){
设map={};
let keys=[“个人材料”、“视频”、“图片”];
array.forEach(元素=>{
设id=element.contactor.id;
设eleTime=element.distributionResult.time;
let object=map[id]
如果(!对象){
对象=映射[id]={
接触器:element.contactor,
distributionResult:element.distributionResult,
sendTime:element.sendTime
}
}否则{
设lastTime=object.Distribution