Variable copy in angular 2 (typescript) [duplicate]

-1

I need to make a copy of an array without using reference

Example

var a = ["teste", "teste1"];
var b = a;

a[0] = "teste3";
b[0] = "teste4";
console.log(a) //mostrar teste3
console.log(b) //mostrar teste4
    
asked by anonymous 04.07.2018 / 13:06

1 answer

2

For arrays using es6 you can do this:

var a = ["teste", "teste1"];
var b =[...a];

a[0] = "teste3";
b[0] = "teste4";

console.log(a) //mostrar teste3
console.log(b) //mostrar teste4
    
04.07.2018 / 13:10