0

Hi guys I have this scenario:

const data = [
    {
       "name": "Hebert",
       "timestamp": 1640371815, // 15:50:15
    },{
       "name": "Brien",
       "timestamp": 1640373855, // 16:24:15
    },{
       "name": "Hebert",
       "timestamp": 1640363601 // 13:33:21 
    },{
       "name": "Mary",
       "timestamp": 1640356701 // 11:38:21
    },{
       "name": "Mary",
       "timestamp": 1640356521 // 11:35:21 
    },{
       "name": "Mary",
       "timestamp": 1640356401 // 11:33:21
   }
];

I need to get lasted one from duplicates by timestamp:

   // expected result
    [
       {
         "name": "Hebert",
         "timestamp": 1640371815 // 15:50:15 newest one
       },{
         "name": "Brien",
         "timestamp": 1640373855 // 16:24:15 unique
       },{
         "name": "Mary",
         "timestamp": 1640356701 // 11:38:21 newest
       }
    ];

I try this:

    data.sort((a,b) => a.name.localeCompare(b.name))
        .filter((chat, index, self) => self.findIndex( c => c.name == chat.name ) == index )

output duplicated removed but no considering newest timestamp between each duplicates

1
  • Sort by timestamp first Commented Dec 24, 2021 at 20:33

3 Answers 3

1

Rather than sorting and using filter you can iterate the array just once storing items in a Map or object using the names as keys and then just update the lowest timestamp as you loop through each item.

Then finally convert the object values back to array

const items = new Map()

data.forEach(({name,timestamp}) => {  
  const o = items.get(name);
  if(o){
    timestamp < o.timestamp && o.timestamp = timestamp;    
  } else {
     items.set(name, {name, timestamp});
  }
});

const res = [...items.values()]

console.log(res)
<script>

const data = [
    {
       "name": "Hebert",
       "timestamp": 1640371815, // 15:50:15
    },{
       "name": "Brien",
       "timestamp": 1640373855, // 16:24:15
    },{
       "name": "Hebert",
       "timestamp": 1640363601 // 13:33:21 
    },{
       "name": "Mary",
       "timestamp": 1640356701 // 11:38:21
    },{
       "name": "Mary",
       "timestamp": 1640356521 // 11:35:21 
    },{
       "name": "Mary",
       "timestamp": 1640356401 // 11:33:21
   }
];
</script>

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

Comments

1

const data = [
    {
       "name": "Hebert",
       "timestamp": 1640371815, // 15:50:15
    },{
       "name": "Brien",
       "timestamp": 1640373855, // 16:24:15
    },{
       "name": "Hebert",
       "timestamp": 1640363601 // 13:33:21 
    },{
       "name": "Mary",
       "timestamp": 1640356701 // 11:38:21
    },{
       "name": "Mary",
       "timestamp": 1640356521 // 11:35:21 
    },{
       "name": "Mary",
       "timestamp": 1640356401 // 11:33:21
   }
];

const seen = {};
const dataModified = [];

data.forEach((elem) => {
  const index = seen[elem.name];
  
  if(index == undefined){
    dataModified.push(elem);
    seen[elem.name] = dataModified.length - 1;
  } else if(dataModified[index].timestamp < elem.timestamp){
    dataModified.splice(index, 1, elem);
  }
});

console.log(dataModified);

Comments

0
data.sort((a,b) => a.name.localeCompare(b.name))
.filter((chat, index, self) => self.findIndex(c => c.name === chat.name && c.timestamp > chat.timestamp)<0)

The idea is that findIndex would only return the index when there exists another duplicate with greater timestamp, else it'd return -1.

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.