[英]How to insert a deleted element at the original position in the array?
In the below code:在下面的代码中:
For some reason in step 3 above (in the FINAL ARR
console log), the elements are being added in the wrong position.由于某些原因,在上面的第 3 步(在
FINAL ARR
控制台日志中),元素被添加到了错误的位置。 Is my slice function incorrect in step 3?我的切片函数在步骤 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);
There's no need to subtract from elem.position
.无需从
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);
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.