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.

1
  • 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. Commented Jan 20, 2020 at 3:47

3 Answers 3

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

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

Comments

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

Comments

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

Comments

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.