Deep clone object in Javascript except Object.assign [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)
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?

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);

How do I deep clone JSON object in javascript [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)
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

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);

Look up object keys by value [duplicate]

This question already has answers here:
Swap key with value in object
(25 answers)
Closed 5 years ago.
Object = {"o":"s","e":"w"}
If I have this object, is there a way to perform reverse lookups on it?
Something like:
Object.invert()["s"]
> "o"
You want to revert the key/value mapping.
var test = {a: "b", c: "d"}
var reverted = {}
for(var key in test) {
reverted[test[key]] = key
}

Javascript array length in click-function [duplicate]

This question already has answers here:
Length of a JavaScript object
(43 answers)
Closed 8 years ago.
I'm having a hard time understanding this code. Could someone try to explain why an array can have elements and 0 length?
var myArray = [];
myArray["hello"] = 4;
myArray["world"] = 65;
$('#btn').on('click',function() {
console.log(myArray.length); // Prints 0
console.log(myArray); // Prints [hello: 4, world: 65]
console.log(myArray.length); // Prints 0
}
The .length property only pertains to numerically-indexed properties.

Categories