Created
July 16, 2017 17:44
-
-
Save arsho/dc3643babe09fa71ede737269d8e443a to your computer and use it in GitHub Desktop.
Array splice implementation to convert an existing array to target array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let sourceElem = [1,2,3,4,5,6]; | |
let targetElem = [1,2,3,4,5,6, | |
1,2,3,4,5,6, | |
2,3,4,5,6, | |
1,3,4,5,6, | |
1,4,5,6]; | |
let resultElem = [ | |
1,2,3,4,5,6, | |
1,2,3,4,5,6, | |
'-',2,3,4,5,6, | |
1,'-',3,4,5,6, | |
1,'-','-',4,5,6 | |
]; | |
let pos = 0; | |
for(let i=0;i<targetElem.length;i++){ | |
if(pos>=sourceElem.length){ | |
pos = 0; | |
} | |
if(targetElem[i]!=sourceElem[pos]){ | |
targetElem.splice(i,0,'-'); | |
} | |
pos+=1; | |
} | |
console.log(targetElem.toString() == resultElem.toString()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment