Home > Enterprise >  How can I sort and reduce the object base on this sample data from javascript array?
How can I sort and reduce the object base on this sample data from javascript array?

Time:05-19

The date and time are integers, I would like to sort and reduce them base on up to date format like date should be up to date and military time should be up to date too. All values are distinct base on there object id

let arr = [
            {"id": 1, "date": 20220518, "time": 1700},
            {"id": 1, "date": 20220518, "time": 1500},
            {"id": 1, "date": 20220518, "time": 2200},
            {"id": 1, "date": 20220517, "time": 2300},
            {"id": 2, "date": 20220518, "time": 1500},
            {"id": 2, "date": 20220516, "time": 2100},
            {"id": 3, "date": 20220518, "time": 1400},
            {"id": 3, "date": 20220517, "time": 1900},
            {"id": 3, "date": 20220518, "time": 1900},
            {"id": 4, "date": 20220517, "time": 1900},
            {"id": 4, "date": 20220516, "time": 1300},
            {"id": 5, "date": 20220516, "time": 1600}
        ]
    
    
    
 // Result should be like this
 let arr = [
            {"id": 1, "date": 20220518, "time": 2200},
            {"id": 2, "date": 20220518, "time": 1500},
            {"id": 3, "date": 20220518, "time": 1900},
            {"id": 4, "date": 20220517, "time": 1900},
            {"id": 5, "date": 20220516, "time": 1600}
        ]

CodePudding user response:

There are so many ways I don't know about your data and what you have done so far. Simple logic is below you can filter the data as below or you can use jquery.

let arr = [{
    "id": 1,
    "date": 20220518,
    "time": 1700
  },
  {
    "id": 1,
    "date": 20220518,
    "time": 1500
  },
  {
    "id": 1,
    "date": 20220518,
    "time": 2200
  },
  {
    "id": 1,
    "date": 20220517,
    "time": 2300
  },
  {
    "id": 2,
    "date": 20220518,
    "time": 1500
  },
  {
    "id": 2,
    "date": 20220516,
    "time": 2100
  },
  {
    "id": 3,
    "date": 20220518,
    "time": 1400
  },
  {
    "id": 3,
    "date": 20220517,
    "time": 1900
  },
  {
    "id": 3,
    "date": 20220518,
    "time": 1900
  },
  {
    "id": 4,
    "date": 20220517,
    "time": 1900
  },
  {
    "id": 4,
    "date": 20220516,
    "time": 1300
  },
  {
    "id": 5,
    "date": 20220516,
    "time": 1600
  }
]

let data=[];
arr.forEach(function(e, i) {
  data[e.id]=e;
});
console.log(data.filter(n => n));

CodePudding user response:

Use Array.prototype.filter()

let arr = [
  { id: 1, date: 20220518, time: 1700 },
  { id: 1, date: 20220518, time: 1500 },
  { id: 1, date: 20220518, time: 2200 },
  { id: 1, date: 20220517, time: 2300 },
  { id: 2, date: 20220518, time: 1500 },
  { id: 2, date: 20220516, time: 2100 },
  { id: 3, date: 20220518, time: 1400 },
  { id: 3, date: 20220517, time: 1900 },
  { id: 3, date: 20220518, time: 1900 },
  { id: 4, date: 20220517, time: 1900 },
  { id: 4, date: 20220516, time: 1300 },
  { id: 5, date: 20220516, time: 1600 },
];
const result = arr.filter(
  (item) =>
    !arr.some(
      (i) =>
        i.id === item.id &&
        // assume the time is a 4 digits number
        i.date * 10000   i.time > item.date * 10000   item.time
    )
);

console.log(result);

Use Array.prototype.reduce()

const result = arr.reduce((previousArray, currentItem) => {
  const item = previousArray.find((i) => i.id === currentItem.id);
  if (item) {
    if (currentItem.date > item.date) {
      item.date = currentItem.date;
      item.time = currentItem.time;
    } else if (currentItem.date === item.date && currentItem.time > item.time) {
      item.time = currentItem.time;
    }
  } else {
    previousArray.push(currentItem);
  }
  return previousArray;
}, []);

console.log(result);
  • Related