score:6
Accepted answer
every js object is passed as a reference (objects, arrays, functions etc.). in order to make a 'deep copy' of a particular object you can do:
var deepcopy = json.parse(json.stringify(oldobject)) // 1. - more of a hack
var deepcopy = _.clonedeep(oldobject); // 2. use some predefined methods from jquery, lodash or any other library
var shallowcopy = object.assign({}, oldobject) // 1. - preferred (if you support new es features)
this way your data on the list won't be modified.
score:10
if you only need a shallow copy you can do it like this:
arr.push({...o})
assuming o is the object that you want to push and clone at the same time.
Source: stackoverflow.com
Related Query
- Javascript push object as clone
- Change the format of JavaScript Data object
- Using D3.js with a javascript object instead of an external JSON file
- Javascript - How to pass original object to callback function
- Javascript manipulate data into array of object for d3 stacked chart
- Convert Javascript object to array of numbers
- javascript convert object to an array
- Javascript Date object returning December 31st 1969
- How do I graph an individual line for each the groups within a Javascript map object using D3.js
- Javascript JQuery : Object doesn't support property or method 'toLowerCase'
- Access subkeys in multi-dimensional JavaScript object
- Upload Javascript Object (svgCrowbar output) to Laravel 5.6 Server
- How to filter JavaScript object based on property condition?
- push array object to new object
- ordering keys from Javascript object
- Animating SVG group object using the JavaScript library
- How to reshaping Data in Javascript from array to object without losing some of the data
- Reversing the Data Array in D3.js Given a JavaScript Object with Key, Val Pair
- Change Javascript Object Array data
- convert time string to date object javascript
- JavaScript object randomly changed?
- associating a javascript object to an SVG element
- Javascript Pie Chart php to javascript object array
- Modified object not indexing properly on mouse click function, javascript
- How to pass JavaScript object to another function?
- Deep clone a function in javascript
- Sum all the values in an object by filtering country attribute - javascript
- javascript populate nested object from other nested object
- Could not group by list item in javascript object
- Javascript returns undefined when indexing into an array using a property of an object
More Query from same tag
- Why point closeness is differing from scatter plot to hexbin plot?
- Node data update d3.js v4 force diagram
- how to make sankyplots with no color for source and target node and how to do legend to show the different colors of linkgroup
- D3js: How do I clear the zoom scale set by the d3.zoom event?
- How does d3.js(version:3) figure out it is a radial tree layout
- d3.js CSV data aggregation
- How to add links from CSV file to SVG elements generated with D3?
- How to create sorted square graph using javascript
- Applying stroke-dasharray style to only part of a path
- Propagating events through overlapping svg elements
- D3 not show Circles
- Some help please in understanding a JavaScript function call. Is it a closure or recursive?
- Clip path is moving with group of elements when using d3.drag
- Creating DC charts dynamically
- Hierarchical Edge Bundling d3 v4 - simplest 2 node example
- Cal-Heatmap Stange (incorrect) color behavior
- Tooltip with Multi-Series Line Chart in Vega-Lite API
- Angular2 component not called when I do d3JS append
- How can we create an onclick event for a d3 data map?
- D3.js Looking for efficient way to hide selective hierarchical elements
- How can I calculate the degree of nodes in D3 v5?
- d3.js : Getting gradients on a bar chart?
- Convert d3v6 Observable example using zoom.center
- d3.nest() Nest array elements with multiple same-level keys (creating duplicates)
- D3 - Can't get packed circles to work from csv
- streamgraph from .csv file in D3
- d3 graph - How do I make the JS get the JSON from PHP script and then select axes and plot?
- Using D3 to draw background gridline paths in a matrix for scatterplots?
- Dimple.JS Animate line series animation
- Only last Y-axis is being updated while dragging on d3 chart