Variable copy in angular 2(typescript)

Asked

Viewed 54 times

-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

1 answer

2


For arrays using es6 vc can do so:

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

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

console.log(a) //mostrar teste3
console.log(b) //mostrar teste4
  • That’s exactly what it was, thank you ^^

Browser other questions tagged

You are not signed in. Login or sign up in order to post.