0

So i have an array with objects which has the basic information such as:

var data = [{
    "id": 31737,
    "mobile": "123456",
    "name": "Jack",
    "time": "2019-12-15 13:52:43"
  },
  {
    "id": 31737,
    "mobile": "123456",
    "name": "Jack",
    "time": "2019-12-14 12:00:00"
  },
  {
    "id": 31737,
    "mobile": "123456",
    "name": "Jack",
    "time": "2019-12-14 09:15:05"
  },
  {
    "id": 88991,
    "mobile": "123456",
    "name": "Mike",
    "time": "2019-12-15 12:10:43"
  },
  {
    "id": 88991,
    "mobile": "123456",
    "name": "Mike",
    "time": "2019-12-15 10:52:43"
  },
  {
    "id": 88991,
    "mobile": "123456",
    "name": "Mike",
    "time": "2019-12-14 09:52:43"
  }
]

As you can see, the array stores the arrival time of each person. Same person has different arrival times, I need to make a function that returns new array, which stores each person's the latest arrival time(record) only. For returning the latest date, i found this answer answer, but can't really figure out how i should do in this situation, where similar id(or person) appears for couple times. How should i iterate so i can know that this timeis Mike's arrival time and not Jack's. Thank you.

ankitkanojia
  • 3,072
  • 4
  • 22
  • 35
casper
  • 259
  • 4
  • 18
  • Build an object that maps names to latest time. Loop through the list. If the name doesn't exist in the object or the time is more recent, add/update the entry in the object. Done. – Niet the Dark Absol Jan 20 '20 at 03:47

3 Answers3

1

took me a few minutes but I think I got it to work, if you click on the stackblitz at the bottom and check the console log you will see it gives the correct output.

var data = [{
    "id": 31737,
    "mobile": "123456",
    "name": "Jack",
    "time": "2019-12-15 13:52:43"
  },
  {
    "id": 31737,
    "mobile": "123456",
    "name": "Jack",
    "time": "2019-12-14 12:00:00"
  },
  {
    id: 31737,
    "mobile": "123456",
    "name": "Jack",
    "time": "2019-12-14 09:15:05"
  },
  {
    id: 88991,
    "mobile": "123456",
    "name": "Mike",
    "time": "2019-12-15 12:10:43"
  },
  {
    id: 88991,
    "mobile": "123456",
    "name": "Mike",
    "time": "2019-12-15 10:52:43"
  },
  {
    id: 88991,
    "mobile": "123456",
    "name": "Mike",
    "time": "2019-12-14 09:52:43"
  }
]

class timeCompare {
  constructor() {}
  payload = {}
  addEntry({
    id,
    time,
    mobile,
    name
  }) {
    if (!this[id]) {
      this.payload[id] = {
        'time': time,
        'name': name,
        'mobile': mobile
      }
    } else {
      var current = this.parseDate(this[id]['time'])
      var check = this.parseDate(time)
      if (check > current) {
        this.payload[id]['time'] = time
      }
    }
  }
  parseDate(dateString) {
    console.dir({
      'test': dateString
    })
    var d = dateString.substr(0, 10).split('-')
    var t = dateString.substr(11, 8)
    return Date.parse(`${d[1]}/${d[2]}/${d[0]} ${t}`)
  }
}

var control = new timeCompare()
data.forEach(_val => {
  control.addEntry(_val)
})
console.log(control)
var output = []
for (let OBJ in control.payload) {
  output.push({
    id: OBJ,
    ...control.payload[OBJ]
  })
}

console.log(output);

here is a working stackblitz

https://stackblitz.com/edit/typescript-3werja

ankitkanojia
  • 3,072
  • 4
  • 22
  • 35
Raphael Castro
  • 907
  • 1
  • 8
  • 26
0

Pass the data variable to this function to get the dates as a new array:

function getTime(dataset){
  var set = dataset.map(function(e){
    return e.time;
  });
  return set;
}

Then call the function as shown below:

var timeSet = getTime(data);
0
let output = data.reduce( (acc, item) => {
    let ids = acc.map(j => j.id);
    if(ids.includes(item.id)) {
        let existingItem = acc.filter( entry => entry.id === item.id);
        existingItem.time =  Date.parse(item.time) > Date.parse(existingItem.time) ? 
                             item.time : existingItem.toLocaleString;
    } else {
    acc.push(item);
    }
    return acc;
}, []);

Note the JSON provided is not well formed, I reformatted it and here is the full Code:

var data = [{
    "id": 31737,
    "mobile": "123456",
    "name": "Jack",
    "time": "2019-12-15 13:52:43"
  },
  {
    "id": 31737,
    "mobile": "123456",
    "name": "Jack",
    "time": "2019-12-14 12:00:00"
  },
  {
    "id": 31737,
    "mobile": "123456",
    "name": "Jack",
    "time": "2019-12-14 09:15:05"
  },
  {
    "id": 88991,
    "mobile": "123456",
    "name": "Mike",
    "time": "2019-12-15 12:10:43"
  },
  {
    "id": 88991,
    "mobile": "123456",
    "name": "Mike",
    "time": "2019-12-15 10:52:43"
  },
  {
    "id": 88991,
    "mobile": "123456",
    "name": "Mike",
    "time": "2019-12-14 09:52:43"
  }
];

let output = data.reduce((acc, item) => {
  let ids = acc.map(j => j.id);
  if (ids.includes(item.id)) {
    let existingItem = acc.filter(entry => entry.id === item.id);
    existingItem.time = Date.parse(item.time) > Date.parse(existingItem.time) ? item.time : existingItem.toLocaleString;
    return acc;
  } else {
    acc.push(item);
    return acc;
  }
}, []);
console.log(output);
ankitkanojia
  • 3,072
  • 4
  • 22
  • 35
David Kou
  • 1
  • 1