1

eu tenho a seguinte lista de objetos porem preciso filtrar apenas o 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 },

Depois rodei esse codigo para tirar os repetidos...

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

Recebi :

[ { 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 } ]

Só que eu gostaria que ele só mostrasse o que tem a data mais atual que tenha o id = id

Não tenho nem ideia de como fazer isso.... alguem poderia ajudar...

1 Resposta 1

2

Para buscar o elemento com a Data mais actual usa o metodo sort, isto é, ordena o array dos users através do campo createdAt descendentemente e depois busca o primeiro elemento do 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)

... que tenha o id = id

Nota que no seu Objecto todos os users tem o mesmo id:5ce82f0deee6dc458a20b21e logo não ví necessidade de agrupar por id, mais, vou deixar um exemplo de como séria.

Exemplo com ids deferentes:

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)

1

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .