Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

191
Visualizações
Conditioning inside array.map() objects

I'm trying to join three array's based on matching id's, but if the posts or likes is "undefined", then it ends up breaking the code, how would I apply conditioning to this, if the array is undefined, it should return empty and if not the filtering should take place.

//sample data
const posts = 
  [ { _id: 1
    , message: 'this is post one'
    , likes:    [ { _id: 1111 } , { _id: 2222 } ] 
    , comments: [ { _id: 3333 } , { _id: 4444 } ] 
    } 
  ] 
const comments = 
  [ { _id: 3333, message: 'this is comment 1' } 
  , { _id: 4444, message: 'this is comment 2' } 
  ] 
const likes = 
  [ { _id: 1111, user: 'Peter' } 
  , { _id: 2222, user: 'John'  } 
  ] 


      const newPosts = posts.map(post => ({
        ...post,
        likes: likes.filter(like => post.likes.map(like => like._id).includes(like._id)),
        comments: comments.filter(comment => post.comments.map(comment => comment._id).includes(comment._id)),
      }))

what I've tried

const newPosts = posts.map(post => ({
          ...post,
          likes: likes.filter(like => like === undefined ? '': post.likes.map(likes => likes._id).includes(like._id)),
          comments: comments.filter(comment => comment === undefined ? '': post.comments.map(comments => comments._id).includes(comment._id)),
        }))

the use for this is socket.io emits changes from my database with the three different sockets, I'm able to grab all three arrays containing the changes and join them into one updated array "posts" by matching ID's, so meaning the comments/likes array can end up being empty from time to time, there by I'd like to apply conditioning that it will check whether it is undefined or not. If it's undefined it should just return the value of an empty array else the filter() action should occur, would this be possible, I've been stuck on this for a bit, so any help would be really appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Try this:

const posts = [{
  _id: 1,
  message: 'this is post one',
  likes: [{
    _id: 1111
  }, {
    _id: 2222
  }],
  comments: [{
    _id: 3333
  }, {
    _id: 4444
  }]
}];

const comments = [{
  _id: 3333,
  message: 'this is comment 1'
}, {
  _id: 4444,
  message: 'this is comment 2'
}];

const likes = [{
  _id: 1111,
  user: 'Peter'
}, {
  _id: 2222,
  user: 'John'
}];

const newPosts = posts.map((post = {}) => ({
  ...post,
  likes: likes.filter((like = {}) =>
    post.likes.find((likeItem = {}) => likeItem._id === like._id)
  ).map(like => like._id),
  comments: comments.filter((comment = {}) =>
    post.comments.find((commentItem = {}) => commentItem._id === comment._id)
  ).map(comment => comment._id),
}));

console.log(newPosts);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda