How to filter and display the date most acts within a JS object

Asked

Viewed 31 times

1

i have the following list of objects but need to filter only user...

let msg = [{ _id: 5cef45879e78602774e921d0,
    to: '5ce82f0deee6dc458a20b21e',
    from: '5ce82f0deee6dc458a20b21e',
    text: '5',
    type: 'text',
    user:
     { id: '5ce82f0deee6dc458a20b21e',
       name: 'usuario4.2',
       display: 'usuario4',
       descript: 'usuario4',
       avatar: 'blazer2.png',
       createdAt: 1559184735135 },
    channel: '/geral',
    createdAt: 1559184775656 },
  { _id: 5cef45789e78602774e921cf,
    to: '5ce82f0deee6dc458a20b21e',
    from: '5ce82f0deee6dc458a20b21e',
    text: '4',
    type: 'text',
    user:
     { id: '5ce82f0deee6dc458a20b21e',
       name: 'usuario4.2',
       display: 'usuario4',
       descript: 'usuario4',
       avatar: 'blazer2.png',
       createdAt: 1559184735135 },
    channel: '/geral',
    createdAt: 1559184760319 },
  { _id: 5cef406c52c9ec1e37065be7,
    to: '5ce82f0deee6dc458a20b21e',
    from: '5ce82f0deee6dc458a20b21e',
    text: '3',
    type: 'text',
    user:
     { id: '5ce82f0deee6dc458a20b21e',
       name: 'usuario4.1',
       display: 'usuario4',
       descript: 'usuario4',
       avatar: 'blazer2.png',
       createdAt: 1559183392625 },
    channel: '/geral',
    createdAt: 1559183468769 },
  { _id: 5cef406952c9ec1e37065be6,
    to: '5ce82f0deee6dc458a20b21e',
    from: '5ce82f0deee6dc458a20b21e',
    text: '2',
    type: 'text',
    user:
     { id: '5ce82f0deee6dc458a20b21e',
       name: 'usuario4.1',
       display: 'usuario4',
       descript: 'usuario4',
       avatar: 'blazer2.png',
       createdAt: 1559183392625 },
  { _id: 5cef406352c9ec1e37065be5,
    to: '5ce82f0deee6dc458a20b21e',
    from: '5ce82f0deee6dc458a20b21e',
    text: '1',
    type: 'text',
    user:
     { id: '5ce82f0deee6dc458a20b21e',
       name: 'usuario4.1',
       display: 'usuario4',
       descript: 'usuario4',
       avatar: 'blazer2.png',
       createdAt: 1559183392625 },
    channel: '/geral',
    createdAt: 1559183459121 } ]

Então rodei let x = msg.map(res => res.user) dai consegui..




{ id: '5ce82f0deee6dc458a20b21e',
   name: 'usuario4.2',
   display: 'usuario4',
   descript: 'usuario4',
   avatar: 'blazer2.png',
   createdAt: 1559184735135 },
 { id: '5ce82f0deee6dc458a20b21e',
   name: 'usuario4.2',
   display: 'usuario4',
   descript: 'usuario4',
   avatar: 'blazer2.png',
   createdAt: 1559184735135 },
 { id: '5ce82f0deee6dc458a20b21e',
   name: 'usuario4.1',
   display: 'usuario4',
   descript: 'usuario4',
   avatar: 'blazer2.png',
   createdAt: 1559183392625 },
 { id: '5ce82f0deee6dc458a20b21e',
   name: 'usuario4.1',
   display: 'usuario4',
   descript: 'usuario4',
   avatar: 'blazer2.png',
   createdAt: 1559183392625 },
 { id: '5ce82f0deee6dc458a20b21e',
   name: 'usuario4.1',
   display: 'usuario4',
   descript: 'usuario4',
   avatar: 'blazer2.png',
   createdAt: 1559183392625 },

Then I circled that code to get the repeaters out...

let unic = x.filter(function (a) {
                    return !this[JSON.stringify(a)] && 
                    (this[JSON.stringify(a)] = true);
                }, Object.create(null))

I received :

[ { id: '5ce82f0deee6dc458a20b21e',
    name: 'usuario4.1',
    display: 'usuario4',
    descript: 'usuario4',
    avatar: 'blazer2.png',
    createdAt: 1559183392625 },
  { id: '5ce82f0deee6dc458a20b21e',
    name: 'usuario4.2',
    display: 'usuario4',
    descript: 'usuario4',
    avatar: 'blazer2.png',
    createdAt: 1559184735135 } ]

Only that I would like it only to show what has the most current date that has id = id

I have no idea how to do this.... someone could help...

1 answer

2


To find the widget with the most current Date uses the method sort, that is, sort the users array through the field createdAt descending and then searching for the first element of the Array.

const ultimoUser = [{
    _id: '5cef45879e78602774e921d0',
    to: '5ce82f0deee6dc458a20b21e',
    from: '5ce82f0deee6dc458a20b21e',
    text: '5',
    type: 'text',
    user: {
      id: '5ce82f0deee6dc458a20b21e',
      name: 'usuario4.2',
      display: 'usuario4',
      descript: 'usuario4',
      avatar: 'blazer2.png',
      createdAt: 1559184735135
    },
    channel: '/geral',
    createdAt: 1559184775656
  },
  {
    _id: '5cef45789e78602774e921cf',
    to: '5ce82f0deee6dc458a20b21e',
    from: '5ce82f0deee6dc458a20b21e',
    text: '4',
    type: 'text',
    user: {
      id: '5ce82f0deee6dc458a20b21e',
      name: 'usuario4.2',
      display: 'usuario4',
      descript: 'usuario4',
      avatar: 'blazer2.png',
      createdAt: 1559184735135
    },
    channel: '/geral',
    createdAt: 1559184760319
  },
  {
    _id: '5cef406c52c9ec1e37065be7',
    to: '5ce82f0deee6dc458a20b21e',
    from: '5ce82f0deee6dc458a20b21e',
    text: '3',
    type: 'text',
    user: {
      id: '5ce82f0deee6dc458a20b21e',
      name: 'usuario4.1',
      display: 'usuario4',
      descript: 'usuario4',
      avatar: 'blazer2.png',
      createdAt: 1559183392625
    },
    channel: '/geral',
    createdAt: 1559183468769
  },
  {
    _id: '5cef406952c9ec1e37065be6',
    to: '5ce82f0deee6dc458a20b21e',
    from: '5ce82f0deee6dc458a20b21e',
    text: '2',
    type: 'text',
    user: {
      id: '5ce82f0deee6dc458a20b21e',
      name: 'usuario4.1',
      display: 'usuario4',
      descript: 'usuario4',
      avatar: 'blazer2.png',
      createdAt: 1559183392625
    }
  },
  {
    _id: '5cef406352c9ec1e37065be5',
    to: '5ce82f0deee6dc458a20b21e',
    from: '5ce82f0deee6dc458a20b21e',
    text: '1',
    type: 'text',
    user: {
      id: '5ce82f0deee6dc458a20b21e',
      name: 'usuario4.1',
      display: 'usuario4',
      descript: 'usuario4',
      avatar: 'blazer2.png',
      createdAt: 1559183392625
    },
    channel: '/geral',
    createdAt: 1559183459121
  }
].map(item => {
  let _user = item.user;
  _user._createdAt_test = new Date(_user.createdAt); // Converte a data para um formato de facil leitura [Só para teste]
  return _user
}).sort((a, b) => { // Aplica ordem descendente no campo 'createdAt' de modo que a data mais actual seja a primeira
  return (new Date(b.createdAt)) - (new Date(a.createdAt))
})[0]; // Busca o primeiro elemento do Array, que neste este caso é o user com a data de criação mais actual

console.log(ultimoUser)

... with id = id

Note that in your Object all users have the same id:5ce82f0deee6dc458a20b21e so I saw no need to group by id, plus, I’ll leave an example of how serious.

Example with deferent ids:

const ultimosUsers = [{
    _id: '5cef45879e7860dsfvsdf2774e921d0',
    to: '5ce82f0deee6dc458a20b21e',
    from: '5ce82f0deee6dc458a20b21e',
    text: '5',
    type: 'text',
    user: {
      id: '5ce82f0deee6dc4dxfdsfdsf58a20b21e',
      name: 'usuario4.2',
      display: 'usuario4',
      descript: 'usuario4',
      avatar: 'blazer2.png',
      createdAt: 1559184735135
    },
    channel: '/geral',
    createdAt: 1559184775656
  },
  {
    _id: '5cef45789e78602774e921cf',
    to: '5ce82f0deee6dc458a20b21e',
    from: '5ce82f0deee6dc458a20b21e',
    text: '4',
    type: 'text',
    user: {
      id: '5ce82f0deee6dc458a20b21e',
      name: 'usuario4.2',
      display: 'usuario4',
      descript: 'usuario4',
      avatar: 'blazer2.png',
      createdAt: 1559184735135
    },
    channel: '/geral',
    createdAt: 1559184760319
  },
  {
    _id: '5cef45879e7860dsfvsdf2774e921d0',
    to: '5ce82f0deee6dc458a20b21e',
    from: '5ce82f0deee6dc458a20b21e',
    text: '5',
    type: 'text',
    user: {
      id: '5ce82f0deee6dc4dxfdsfdsf58a20b21e',
      name: 'usuario4.2',
      display: 'usuario4',
      descript: 'usuario4',
      avatar: 'blazer2.png',
      createdAt: 1559184735135
    },
    channel: '/geral',
    createdAt: 1559184775656
  },
  {
    _id: '5cef45789e78602774e921cf',
    to: '5ce82f0deee6dc458a20b21e',
    from: '5ce82f0deee6dc458a20b21e',
    text: '4',
    type: 'text',
    user: {
      id: '5ce82f0deee6dc458a20b21e',
      name: 'usuario4.2',
      display: 'usuario4',
      descript: 'usuario4',
      avatar: 'blazer2.png',
      createdAt: 1559184735135
    },
    channel: '/geral',
    createdAt: 1559184760319
  },
  {
    _id: '5cef406csdfcdsfds52c9ec1e37065be7',
    to: '5ce82f0deee6dc458a20b21e',
    from: '5ce82f0deee6dc458a20b21e',
    text: '3',
    type: 'text',
    user: {
      id: '5ce82f0deee6dc458a20b21e',
      name: 'usuario4.1',
      display: 'usuario4',
      descript: 'usuario4',
      avatar: 'blazer2.png',
      createdAt: 1559183392625
    },
    channel: '/geral',
    createdAt: 1559183468769
  },
  {
    _id: '5cef406952c9ec1e37065be6',
    to: '5ce82f0deee6dc458a20b21e',
    from: '5ce82f0deee6dc458a20b21e',
    text: '2',
    type: 'text',
    user: {
      id: '5ce82f0deee6dc4dxfdsfdsf58a20b21e',
      name: 'usuario4.1',
      display: 'usuario4',
      descript: 'usuario4',
      avatar: 'blazer2.png',
      createdAt: 1559183392625
    }
  },
  {
    _id: '5cef406352c9ec1e37065be5',
    to: '5ce82f0deee6dc458a20b21e',
    from: '5ce82f0deee6dc458a20b21e',
    text: '1',
    type: 'text',
    user: {
      id: '5ce82f0deee6dc458a20b21e',
      name: 'usuario4.1',
      display: 'usuario4',
      descript: 'usuario4',
      avatar: 'blazer2.png',
      createdAt: 1559183392625
    },
    channel: '/geral',
    createdAt: 1559183459121
  }
].map(item => {
  let _user = item.user;
  _user._createdAt_test = new Date(_user.createdAt); // Converte a data para um formato de facil leitura [Só para teste]
  return _user
}).reduce((accumulator, currentValue) => {
  let lastValue = accumulator[currentValue.id];
  if (lastValue) {
    accumulator[currentValue.id] = [lastValue, currentValue].sort((a, b) => (new Date(b.createdAt)) - (new Date(a.createdAt)))[0]; // Compara os elementos e guarda o elemnto com a Data mas actual
  } else {
    accumulator[currentValue.id] = currentValue;
  }
  return accumulator;
}, {});

console.log(ultimosUsers)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.