How do I deep clone JSON object in javascript [duplicate] - javascript

This question already has answers here:
What is the most efficient way to deep clone an object in JavaScript?
(67 answers)
How to Deep clone in javascript
(25 answers)
How do I correctly clone a JavaScript object?
(81 answers)
Closed 3 years ago.
const obj1 = { food: 'pizza', car: 'ford' };
let obj2 = obj1;
In the above example, I want to do deep copy of obj1 in obj2 .

use _.cloneDeep
https://lodash.com/docs/#cloneDeep
var objects = [{ 'a': 1 }, { 'b': 2 }];
var deep = _.cloneDeep(objects);
console.log(deep[0] === objects[0]);
// => false

Related

ReactJs Combine Key and Value [duplicate]

This question already has answers here:
Merge keys array and values array into an object in JavaScript
(14 answers)
merge two arrays (keys and values) into an object [duplicate]
(3 answers)
Closed 2 years ago.
Platform: ReactJS
I am trying to combine the two arrays as a date:value object. Any thoughts?
a=["1/1/2020", "1/2/2020", "1/3/2020", "1/4/2020"]
b = [ 1, 2, 3, 4 ]
I am looking for the following result:
["1/1/2020":1, "1/2/2020":2, "1/3/2020":3, "1/4/2020":4]
Thank you,
You can try this approach.
const a=["1/1/2020", "1/2/2020", "1/3/2020", "1/4/2020"]
const b = [ 1, 2, 3, 4 ]
const c = {};
a.forEach((v, i) => {
c[v] = b[i]
});
console.log(c);

Deep clone object in Javascript except Object.assign [duplicate]

This question already has answers here:
What is the most efficient way to deep clone an object in JavaScript?
(67 answers)
How to Deep clone in javascript
(25 answers)
Closed 3 years ago.
Object.assign doesn't deep copy an object. considering following code it should print 0 0 0 1 but it is printing 0 0 1 1.
var obj = {
"value": {
"default": 0
}
};
var newo = Object.assign({}, obj);
console.log(obj.value.default);
console.log(newo.value.default);
newo.value.default = 1;
console.log(obj.value.default);
console.log(newo.value.default);
I know we can use JSON.parse(JSON.stringify(obj)). but is that a best practice solution?

Creating a nested Javascript Object from an array of Strings [duplicate]

This question already has answers here:
Convert list of Objects when the levels are given in an array
(2 answers)
Populate nested object from array?
(2 answers)
Closed 3 years ago.
What's the best way to create a N levels nested object (where N is the size of the array) for example:
const arr = ['a','b','c','d']
The output object should look like this:
{
a: {
b: {
c: {
d: true
}
}
}
}
You can use array.reduce, it helps you pass an accumulator where you can accumulate your nested obj.
const array = ['a','b','c','d'];
const object = {};
array.reduce((o, s) => {
return o[s] = {};
}, object);
console.log(object);

Destructure object into a smaller object [duplicate]

This question already has answers here:
One-liner to take some properties from object in ES 6
(12 answers)
Elegant way to copy only a part of an object [duplicate]
(7 answers)
Closed 4 years ago.
I want to create a new object based on anther object but with fewer properties.
I know I can do it by manually assigment like this:
const obj = {
a: 1,
b: 2,
c: 3
};
const smallObj = {
a: obj.a
};
console.log(smallObj)
Is there a way to do it with destructuring?
I have tried doing this:
const obj = {
a: 1,
b: 2,
c: 3
};
const smallObj = {
a
} = {...obj}
console.log(smallObj, a)
But as you can see, I get the variable a to be equal to 1 but smallObj is a reference to obj.

destructure object in javascript es6 when keys are integers [duplicate]

This question already has answers here:
Object destructuring with property names that are not valid variable names
(2 answers)
Closed 6 years ago.
When we have an object in JS like
var obj = {a: "apple", p: "pen"};
then we can destructure it as follows
var {a, p} = obj; /* a = 'apple', p = 'pen' */
i want to know in case when keys are integers, how can we destructure it ? since we cannot declare integers as variable name
var obj = {0: 'pineapple', 1: 'pen'};
Just like any other assigning to new variable names
var {0:a, 1:b} = obj;

Categories