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

286
Visualizações
what is the difference between splice and filter for the use in delete?

members is a prop that come to the child component from parent component when i use splice on copymembers a copy of members it is getting updated in parent component

copyMembers = [...members]
copyMembers[memberIndex].notes.splice(noteIndex,1);

but instead of doing it like above if i use filter then it is not getting updated in parent component why is this happening what is the difference between them

copyMembers = [...members]
copyMembers[memberIndex].notes = copyMembers[memberIndex].notes.filter((item : any) => item.notesId !== notesId)
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place [1]

This explains the behavior you'e seeing with splice. You're passing the data down into the child and the child us updating actual array reference, thus the parent component is being updated.

The filter() method creates a new array with all elements that pass the test implemented by the provided function [2]

When you use filter, this doesn't happen because you're creating a new array with all of the elements that pass the criteria.

[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

[2] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

about 4 years ago · Juan Pablo Isaza Relatório

0

When you copy an array in JS it only does a shallow copy. This means that nested properties share the same reference, in your case notes is shared by reference so if you modify it in the copy it gets modified in the source.

splice() (mutable) modifies the array, and hence also the copied array since they share same reference. filter() (immutable) creates a new array and does not modify the original one.

about 4 years ago · Juan Pablo Isaza Relatório

0

The filter() method creates a new array with all elements that pass the test implemented by the provided function. The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements and returns the removed item(s). splice() changes original array

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