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

136
Visualizações
How to change one array same as another array

I have two arrays. I want compare modified array with original array and I want to change the modified array same as the original array. position want to be same also

I am expecting following output is, my inputs are I have put in snippet

const modified = [
                    {label: 'cover1', formUid: 1045},
                    {label: 'cover2', formUid: 211},
                    {label: 'cover3', formUid: 204},
                    {label: 'cover4', formUid: 258},
                    {label: 'cover1', formUid: 1045},
                ]

but now I am getting like this output, this is not show the {label: 'cover1', formUid: 1045}, this object in 0th index

const modified = [
                    {label: 'cover2', formUid: 211},
                    {label: 'cover3', formUid: 204},
                    {label: 'cover4', formUid: 258},
                    {label: 'cover1', formUid: 1045},
                 ]

    const original = [
               {label: 'cover1', formUid: 1045},
               {label: 'cover2', formUid: 211},
               {label: 'cover3', formUid: 204},
               {label: 'cover4', formUid: 258},
               {label: 'cover1', formUid: 1045},
            ];
                           
    const modified = [
               {label: 'cover4', formUid: 258},
               {label: 'cover2', formUid: 211},
               {label: 'cover3', formUid: 204},
               {label: 'cover1', formUid: 1045},
               {label: 'cover1', formUid: 1045},
            ]
        
function testFunc(original , modified){
 let originalCopyForPosision;
      
 originalCopyForPosision = [...original];
             
 modified.forEach((element,idx) => {
                 
    let targetIndex = originalCopyForPosision.findIndex(e => e.formUid === element.formUid)
                 
     if(idx === targetIndex){
            return
       }else{
                 
            if(targetIndex > 0){
               modified.splice(targetIndex,  1);
               modified.splice(idx,0,element);
            }
                   
         }
        
      })
        
  document.getElementById("originalArray").innerHTML = JSON.stringify(original);
   document.getElementById("modifiedArray").innerHTML = JSON.stringify(modified);
        
}testFunc(original,modified)
<p id="originalArray"></p>
<p id="modifiedArray"></p>

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

0

This is my suggestion

    const original = [
  { label: "cover1", formUid: 1045 },
  { label: "cover2", formUid: 211 },
  { label: "cover3", formUid: 204 },
  { label: "cover4", formUid: 258 },
  { label: "cover1", formUid: 1045 },
];

const modified = [
  { label: "cover2", formUid: 211 },
  { label: "cover1", formUid: 204 },
  { label: "cover4", formUid: 258 },
  { label: "cover3", formUid: 1045 },
  { label: "cover1", formUid: 1045 },
];

function test() {
  let modifiedCopy = modified.slice();
  original.forEach((value, i) => {
    if (
      value.formUid !== modifiedCopy[i].formUid
    ) {
      modifiedCopy[i] = original[i];
    }
  });
  console.log(modified, "\n\n", original, modifiedCopy);
}

test();

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