This question has already been answered:
Hello! Help copy and modify an element in an array. When I copy everything is fine, but as soon as I update 2 elements change. The one that copied and the one from which copied.
There is such an example:
$(function() { var element = [ {"id":1, "name":"test1"}, {"id":2, "name":"test2"}, {"id":3, "name":"test3"}, {"id":4, "name":"test4"}, {"id":5, "name":"test5"} ]; $("#clone").on("click", function(){ element.push(element[1]); }); $("#update").on("click", function(){ element[5]["name"] += " update"; //Изменяется новый элемент и элемент с id 2 console.log(element); }); }); I also try to change:
element.push(element[1]); on:
element[element.length]=element[1].slice(); I get the error: element [1] .slice is not a function