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

223
Visualizações
Insert nested arrays if nested array does not exist

I'm trying to insert new data if the does not exist in the nested array, so far I'm able to add if they don't exist, but it's removing the previous data, any help would be appreciated.

Arrays

const posts = 
  [ { _id: 1
    , message: 'this is post one'
    , likes:    [ { id: 111221, user: 'random 1' } , { id: 2223422, user:'random 2' } ] 
    , comments: [ { id: 332233, message: 'random comment' } , { id: 2344444, message:'another random comment' } ] 
  }] 
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'  } 
  ] 

expected output

const newPost = 
  [ { _id: 1
    , message: 'this is post one'
    , likes:    [ 
        { id: 1111, user: 'Peter' } ,
        { id: 111221, user: 'random 1' } , 
        { id: 2223422, user:'random 2' } ,
        { id: 2222, user: 'John'  }
    ] 
    , comments: [ 
        { id: 3333, message: 'this is comment 1' } ,
        { id: 4444, message: 'this is comment 2' },
        { id: 332233, message: 'random comment' } , 
        { id: 2344444, message:'another random comment' } 
    ] 
  }] 

what I've tried

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)),
}))

console.log(newPosts)
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Perhaps I'm too tired, but I would just merge the arrays together. Your post didn't mention that you needed to check whether the comments in the variable comments already could exist in posts[0].comments (_id: 1).

const posts = [{
  _id: 1,
  message: 'this is post one',
  likes: [{
    id: 111221,
    user: 'random 1'
  }, {
    id: 2223422,
    user: 'random 2'
  }],
  comments: [{
    id: 332233,
    message: 'random comment'
  }, {
    id: 2344444,
    message: 'another random comment'
  }]
}]

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 addTo = (posts, comments, likes) => {
  let newPostInstance = [...posts][0];
  
  newPostInstance.comments = [...newPostInstance.comments, ...comments];
  newPostInstance.likes    = [...newPostInstance.likes, ...likes];
  
  return newPostInstance;
}

const newPost = addTo(posts, comments, likes);

console.log(newPost)

about 4 years ago · Juan Pablo Isaza Relatório

0

const posts1 = 
  [ { _id: 1
    , message: 'this is post one'
    , likes:    [ { id: 111222221, user: 'random 1' } , { id: 2223422, user:'random 2' } ] 
    , comments: [ { id: 3322222233, message: 'random comment' } , { id: 2344444, message:'another random comment' } ] 
  }] 


  const posts = 
  [ { _id: 1
    , message: 'this is post one'
    , likes:    [ { id: 111221, user: 'random 1asdasdasd' } , { id: 2223422, user:'random 2asdasdasd' } ] 
    , comments: [ { id: 332233, message: 'rasdasdasd' } , { id: 2344444, message:'asdasdasdwwwwww' } ] 
  }] 

if(posts.filter(r => r.comments) !== posts1.filter(r => r.comments)){
    const a = posts.map(({likes,_id,message, ...o}) => ({...o.comments}))
    const b = posts1.map(({likes,_id,message, ...o}) => ({...o.comments}))
    const c = posts.map(({comments,_id,message, ...o}) => ({...o.likes}))
    const d = posts1.map(({comments,_id,message, ...o}) => ({...o.likes}))

    const newComments = [...a,...b]
    const newLikes = [...c,...d]

    const returnFlattenObject = (arr) => {
        const flatObject = {};
        for(let i=0; i<arr.length; i++){
           for(const property in arr[i]){
              flatObject[`${property}_${i}`] = arr[i][property];
           }
        };
        return flatObject;
    }

    posts.map(({comments,likes, ...o}) => ({
        ...o,
        comments: returnFlattenObject(newComments),
        likes: returnFlattenObject(newLikes)
    }))
} else {
    console.log('exists')
}

result

[{…}]
0:
comments:
0_0: {id: 332233, message: 'rasdasdasd'}
0_1: {id: 3322222233, message: 'random comment'}
1_0: {id: 2344444, message: 'asdasdasdwwwwww'}
1_1: {id: 2344444, message: 'another random comment'}
[[Prototype]]: Object
likes:
0_0: {id: 111221, user: 'random 1'}
0_1: {id: 111222221, user: 'random 1'}
1_0: {id: 2223422, user: 'random 2'}
1_1: {id: 2223422, user: 'random 2'}
[[Prototype]]: Object
message: "this is post one"
_id: 1
[[Prototype]]: Object
length: 1
[[Prototype]]: Array(0)
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