Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 326 Vote(s) - 3.66 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Nodejs: how to clone an object

#11
Object.assign hasn't been mentioned in any of above answers.

let cloned = Object.assign({}, source);

If you're on ES6 you can use the spread operator:

let cloned = { ... source };


Reference:

[To see links please register here]

Reply

#12
It's hard to do a generic but useful clone operation because what should be cloned recursively and what should be just copied depends on how the specific object is supposed to work.

Something that may be useful is

function clone(x)
{
if (x === null || x === undefined)
return x;
if (typeof x.clone === "function")
return x.clone();
if (x.constructor == Array)
{
var r = [];
for (var i=0,n=x.length; i<n; i++)
r.push(clone(x[i]));
return r;
}
return x;
}

In this code the logic is

- in case of `null` or `undefined` just return the same (the special case is needed because it's an error to try to see if a `clone` method is present)
- does the object have a `clone` method ? then use that
- is the object an array ? then do a recursive cloning operation
- otherwise just return the same value

This clone function should allow implementing custom clone methods easily... for example

function Point(x, y)
{
this.x = x;
this.y = y;

...
}

Point.prototype.clone = function()
{
return new Point(this.x, this.y);
};



function Polygon(points, style)
{
this.points = points;
this.style = style;

...
}

Polygon.prototype.clone = function()
{
return new Polygon(clone(this.points),
this.style);
};

When in the object you know that a correct cloning operation for a specific array is just a shallow copy then you can call `values.slice()` instead of `clone(values)`.

For example in the above code I am explicitly requiring that a cloning of a polygon object will clone the points, but will share the same style object. If I want to clone the style object too instead then I can just pass `clone(this.style)`.
Reply

#13
Objects and Arrays in JavaScript use call by reference, if you update copied value it might reflect on the original object.
To prevent this you can deep clone the object, to prevent the reference to be passed, using lodash library [cloneDeep][1] method
run command
> npm install lodash


const ld = require('lodash')
const objectToCopy = {name: "john", age: 24}
const clonedObject = ld.cloneDeep(objectToCopy)



[1]:

[To see links please register here]

Reply

#14
In Node.js 17.x was added the method `structuredClone()` to allow made a deep clone.

Documentation of reference:

[To see links please register here]

Reply

#15
Try this module for complex structures, developed especially for nodejs -

[To see links please register here]


Works faster than JSON stringify and parse on large structures and supports BigInt.
Reply

#16
How about this method

const v8 = require('v8');
const structuredClone = obj => {
return v8.deserialize(v8.serialize(obj));
};
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through