Ajude a Asast
Patrocinadores
Últimos assuntos
» 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

» reactJS - A JavaScript library for building user interfaces
How to clone an object in Javascript EmptyQua Out 24, 2018 3:02 am por Admin

» Count number of lines in all files existing
How to clone an object in Javascript EmptyQui Jun 28, 2018 4:53 am por Admin

AdSense
Contributo

How to clone an object in Javascript

Ir em baixo

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

Mensagem  Admin em 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 : 41

Ver perfil do usuário http://forum-informatico.forumeiros.com

Voltar ao Topo Ir em baixo

Voltar ao Topo

- Tópicos similares

 
Permissão deste fórum:
Você não pode responder aos tópicos neste fórum