1

So I have this javascript task, where I need to process data in an array. 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. 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" (i.e. 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: console output
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 (i.e Jack). So there is 4 Jacks, and it seems all the fields (contactor, distributionResult, sendTime) getting merged. All i need to be merged is "pics" and "videos".

1
  • 2
    Please don't double-space all the posted code. Commented Jan 21, 2020 at 2:22

1 Answer 1

1

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));

Sign up to request clarification or add additional context in comments.

7 Comments

thnx man, I've met another problem, would you mind to take a look at the updated version of my question
@casper I've updated my answer. If this is what you're looking for, please hit that check for me.
yes sir! but the time value is messed up again. It's not the biggest value
@casper Updated.
How could i make an output look like a template ? I mean the output order :(1)"personnelMateriaRel" (2) "videos" (3) 'contactor' (4) "pics" (5) "distributionResult" (6) "sendTime"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.