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

240
Views
How to insert a deleted element at the original position in the array?

In the below code:

  1. First I create an array of 10 elements
  2. Next I remove 3 random elements from that array.
  3. I try to add those 3 random elements into the original array in the original positions.

For some reason in step 3 above (in the FINAL ARR console log), the elements are being added in the wrong position. Is my slice function incorrect in step 3? If so, how do I modify it to be correct?

// Step 1. Create array of 10 elements
const originalValues = maxAmount => {
  let finalArray = [];
  for (let i = 0; i < maxAmount; i++) {
    finalArray.push({
      key1: `key1-${i+1}`,
      position: i
    });
  }
  return finalArray;
}
let finalArr = originalValues(10);
const finalArrOriginalLength = finalArr.length;

//Step 2. Remove 3 random elements in this case key1-2, key1-7, key1-8
const removedElements = [];
finalArr = finalArr.filter(elem => {
  if (elem.key1 === 'key1-7' || elem.key1 === 'key1-2' || elem.key1 === 'key1-8') {
    removedElements.push(elem);
    return false;
  } else {
    return true;
  }
});

/* Step 3. Add those 3 elements back to the original positions */
removedElements.forEach(elem => {
  finalArr.splice(elem.position - (finalArrOriginalLength - finalArr.length) + 1, 0, elem);
});

/* The inserted elements from step 3 are being inserted in the wrong position as seen when this console.log prints out finalArr */
console.log('FINAL ARR', finalArr);

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

There's no need to subtract from elem.position.

If you sort the removed elements by the original positions, you should be able to insert them at those positions.

This assumes there haven't been any other changes to the array.

// Step 1. Create array of 10 elements
const originalValues = maxAmount => {
  let finalArray = [];
  for (let i = 0; i < maxAmount; i++) {
    finalArray.push({
      key1: `key1-${i+1}`,
      position: i
    });
  }
  return finalArray;
}
let finalArr = originalValues(10);
const finalArrOriginalLength = finalArr.length;

//Step 2. Remove 3 random elements in this case key1-2, key1-7, key1-8
const removedElements = [];
finalArr = finalArr.filter(elem => {
  if (elem.key1 === 'key1-7' || elem.key1 === 'key1-2' || elem.key1 === 'key1-8') {
    removedElements.push(elem);
    return false;
  } else {
    return true;
  }
});

/* Step 3. Add those 3 elements back to the original positions */
removedElements.sort((a, b) => a.position - b.position).forEach(elem => {
  finalArr.splice(elem.position, 0, elem);
});

console.log('FINAL ARR', finalArr);

about 4 years ago · Juan Pablo Isaza Report

0

Try this...

// Step 1. Create array of 10 elements
const originalValues = maxAmount => {
  let finalArray = [];
  for (let i = 0; i < maxAmount; i++) {
    finalArray.push({
      key1: `key1-${i+1}`,
      position: i
    });
  }
  return finalArray;
}
let finalArr = originalValues(10);
const finalArrOriginalLength = finalArr.length;

//Step 2. Remove 3 random elements in this case key1-2, key1-7, key1-8
const removedElements = [];
finalArr = finalArr.filter(elem => {
  if (elem.key1 === 'key1-7' || elem.key1 === 'key1-2' || elem.key1 === 'key1-8') {
    removedElements.push(elem);
    return false;
  } else {
    return true;
  }
});

// Step 3. Add those 3 elements back to the original positions

removedElements
.sort((a,b)=>{return a.position > b.position ? 0 : 1 });

removedElements.forEach(elem => {
  finalArr.splice(elem.position, 0, elem);
});

/* The inserted elements from step 3 are being inserted in the wrong position as seen when this console.log prints out finalArr */
console.log('FINAL ARR', finalArr);

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!