forum-informatico
Gostaria de reagir a esta mensagem? Crie uma conta em poucos cliques ou inicie sessão para continuar.
Ajude a Asast
Patrocinadores
Últimos assuntos
» Artigo ethereum
How to clone an object in Javascript EmptyTer Set 24, 2019 1:16 pm por jpmartins

» Artigo Bitcoin
How to clone an object in Javascript EmptyTer Set 24, 2019 1:13 pm por jpmartins

» Novo site para desportistas / New site for athletes
How to clone an object in Javascript EmptyTer Dez 04, 2018 8:19 am por Admin

» Check used port with the command line
How to clone an object in Javascript EmptyTer Dez 04, 2018 8:10 am por Admin

» Check the port where mariaDB is running
How to clone an object in Javascript EmptySeg Dez 03, 2018 4:59 am por Admin

» Check the linux version
How to clone an object in Javascript EmptySeg Dez 03, 2018 3:12 am por Admin

» Granting rights to a database for a user
How to clone an object in Javascript EmptyQui Nov 29, 2018 2:04 am por Admin

» Create new user
How to clone an object in Javascript EmptyQui Nov 29, 2018 2:01 am por Admin

» Change level of password security level
How to clone an object in Javascript EmptyQui Nov 29, 2018 1:59 am por Admin

AdSense
Contributo

How to clone an object in Javascript

Ir para baixo

How to clone an object in Javascript Empty How to clone an object in Javascript

Mensagem  Admin Ter Jun 07, 2016 3:36 am

Imagine you have a source array, you want to create another one based on the source and want to manipulate the new one.

Código:


var source = [1,2,3];
var new = source;

new[1] = 44;

=> source will be = [1,44,3];
=> new will be = [1,44,3];


Maybe it is not exactly what you want.
You maybe would prefer to obtain a result like:

=> source will be = [1,2,3];
=> new will be = [1,44,3];

In order to do that, you have to clone the source object.
Código:


var source = [1,2,3];
var copy = JSON.parse(JSON.stringify(source));


Than, you will obtain the wanted behavior.
Admin
Admin
Admin

Mensagens : 607
Data de inscrição : 21/01/2008
Idade : 47

https://forum-informatico.forumeiros.com

Ir para o topo Ir para baixo

Ir para o topo

- Tópicos semelhantes

 
Permissões neste sub-fórum
Não podes responder a tópicos