Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!