Skip to content

Instantly share code, notes, and snippets.

@arsho
Created July 16, 2017 17:44
Show Gist options
  • Save arsho/dc3643babe09fa71ede737269d8e443a to your computer and use it in GitHub Desktop.
Save arsho/dc3643babe09fa71ede737269d8e443a to your computer and use it in GitHub Desktop.
Array splice implementation to convert an existing array to target array
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