简体   繁体   English

在 JavaScript 中处理数组

[英]Processing array in JavaScript

So I have this javascript task, where I need to process data in an array.所以我有这个javascript任务,我需要在其中处理数组中的数据。 Suppose given array down below:假设给定的数组如下:

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
    },
]

This array contains objects.该数组包含对象。 So For each object we have:所以对于每个对象,我们有:

personnelMateriaRel;  videos;  contactor (which includes: id, mobile, name, otherTel2, temobile, workUnit); pics; distributionResult(which includes: latitude, longitude, time, content, address); sendTime

Here i have two objects Jack (ID=18320) and Mike (ID=31903), you can see that each object repeats for couple time.这里我有两个对象 Jack (ID=18320) 和 Mike (ID=31903),你可以看到每个对象重复了几次。 It's because they have different "time".这是因为他们有不同的“时间”。 So the task is, return new array (or other data structures) which contains the objects with the last "time" (ie the biggest "time" value) and merge all "pics" and "videos", all objects that have been deleted, with the object with biggest "time" value.所以任务是,返回包含具有最后“时间”(即最大“时间”值)的对象的新数组(或其他数据结构)并合并所有“图片”和“视频”,所有已删除的对象,具有最大“时间”值的对象。 So for above example, the correct value to be stored would be:因此,对于上面的示例,要存储的正确值将是:

  /*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
    }

Here what I've tried so far, but the result still not correct:到目前为止,我已经尝试过,但结果仍然不正确:

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])
}

Would appreciate any help!将不胜感激任何帮助!

UPDATE So thnx to (@thingEvery)'s answer, according to his code, i get the result down below:更新所以thnx到(@thingEvery)的答案,根据他的代码,我得到的结果如下: 控制台输出
The "time" field is correct, but i still get returned all objects who's "time" was less than the biggest "time". “时间”字段是正确的,但我仍然返回所有“时间”小于最大“时间”的对象。 The image shown is for the first Object (ie Jack).显示的图像是第一个对象(即杰克)。 So there is 4 Jacks, and it seems all the fields (contactor, distributionResult, sendTime) getting merged.所以有 4 个插孔,似乎所有字段(联系人、分发结果、发送时间)都合并了。 All i need to be merged is "pics" and "videos".我需要合并的只是“图片”和“视频”。

If I'm understanding your question correctly, you were almost there.如果我正确理解你的问题,你就快到了。 All I had to do to make it work was add "contactor" to your list of keys and fix your time comparison.我所要做的就是将“接触器”添加到您的键列表中并修复您的时间比较。

 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 }, ] function resolve(array) { let map = {}; let keys = ["personnelMateriaRel", "videos", "pics"]; array.forEach(element => { let id = element.contactor.id; let eleTime = element.distributionResult.time; let object = map[id] if (!object) { object = map[id] = { contactor: element.contactor, distributionResult: element.distributionResult, sendTime: element.sendTime } } else { let lastTime = object.distributionResult.time; if (eleTime >= lastTime) { object.contactor = element.contactor; object.distributionResult = element.distributionResult; object.distributionResult.time = eleTime; object.sendTime = element.sendTime; } } 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]) } console.log(resolve(data));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM