I'm working on a Angular 5 project and I can't seem to get array copies that aren't memory pointers. Or rather I can, but sometimes. and there's no distinguishing factor in my eyes to why one would work and another no.
Here's one that doesn't :
detailsOriginal = [
{title: 's ', value: 'sd' },
{title: 'yy : ', value: 'sdgf'},
{title: 'yyy: ', value: 'sdgsed'},
{title: 'hgjtgj : ', value: 'sqdgqsd'},
{title: 'dsfs: ', value: 'sqdg'},
{title: 'dsfds : ', value: 'sqdfgqds'},
{title: 'q : ', value: '1sqdfg'},
{title: 'qsdqsdqsd : ', value: 'sdqffgdsq'},
{title: 'qsdqsd: ', value: 'qdsf'},
{title: 'qs qsd: ', value: 'sqdqs'},
{title: 'qsd: ', value: '2'}
];
treatInfoArray(selection){
console.log(this.detailsOriginal); // correct output. unaltered
let dt = this.detailsOriginal.slice(); // this suposedly copies rather
// than creates a pointer...
setTimeout(()=>{
console.log('new var', dt); // correct output. unaltered
},1);
setTimeout(()=>{
dt.forEach(x => {
x.value = selection[x.value];
});
},20);
setTimeout(()=>{
console.log('modified', dt); // correct. modified.
},50);
setTimeout(()=>{
console.log('original', this.detailsOriginal);// why the hell is this also modified??
},55);
}
Is the issue that the splice is happening in a function rather than the root of the class, that it has to be a global variable?