Partially replace value of key-val pair in an object [duplicate] - javascript

This question already has answers here:
Es6 merge two objects using spread not working
(5 answers)
How to deep merge instead of shallow merge?
(47 answers)
How can I merge properties of two JavaScript objects dynamically?
(69 answers)
Closed 11 months ago.
Given I have the following object:
let obj = {
cat: "paws",
dogs: {
lab: "Christopher",
otherLab: "Lady Buba"
}
}
let objTwo = {
dogs: {
lab: "some new name"
}
}
How can I merge the above two objects so that the final result is:
{
cat: "paws",
dogs: {
lab: "some new name",
otherLab: "Lady Buba"
}
}
I've attempted to use spread operators with let finalObj = {...obj, ...objTwo}, but this entirely replaces the value of dogs, so that the end result is:
{
cat: "paws",
dogs: {
lab: "some new name",
}
}
which isn't what I want.

Related

Redefine object properties keys starting with prefix [duplicate]

This question already has answers here:
What's the quickest way to rename all property names in a Javascript object?
(3 answers)
How to remove part of a string?
(7 answers)
JavaScript: Object Rename Key
(35 answers)
Closed 2 months ago.
If I have the following object, how can I redefine the keys to remove the prefix "Class"?
{
ClassName: "John",
ClassEmail: "john#doe.com",
ClassPhone: "1234567"
}
so it becomes
{
Name: "John",
Email: "john#doe.com",
Phone: "1234567"
}
Is there any easy way?
const obj = {
ClassName: "John",
ClassEmail: "john#doe.com",
ClassPhone: "1234567"
}
const newObj = Object.entries(obj).reduce((acc, item) => ({ ...acc,
[item[0].replace(/Class/g, "")]: item[1]
}), {})
console.log(newObj)

Insert object into dynamic key nested array [duplicate]

This question already has answers here:
Iterate through object properties
(31 answers)
Add property to an array of objects
(7 answers)
Closed 11 months ago.
Desired result: Insert an object into each dynamicItem and have the full obj object.
const obj = {
item1: {},
item2: {
dynamicItem1: [{ id: 'one' }, ], // add another prop here {type: 'added'}
dynamicItem2: [{ id: 'one' }, ] // add another prop here {type: 'added'}
},
}
My attempts: I managed to insert the object, but i can't think how to rebuild the full object and it also feels like i'm doing it wrong already.
const result = Object.keys(obj.item2).map(key => {
return obj.item2[key].map(item => ({ ...item, type: 'added' }))
})

How to deep copy an object using recursion in JavaScript by making use of Object.create()? [duplicate]

This question already has answers here:
What is the most efficient way to deep clone an object in JavaScript?
(67 answers)
Closed 1 year ago.
This is my object
const person = {
name: "Vikram",
age: 21,
place: {
country: "India",
state: "Karnataka",
},
marks: [1, 2, 3, 4],
parents: {
father: "Ramesh",
mother: "Suma",
},
};
function cloneDeep(obj){}
that should create a deep copy of the provided object using recursion and methods like Object.create etc. Please help me to deep copy this object using recursion.
Yes, you can alter the Object class prototype:
if (!Object.prototype.hasOwnProperty.call(Object.prototype, "deepClone")) {
Object.prototype.deepClone = () => {
console.log('clone!')
};
}
const obj1 = { x: 1 };
obj1.deepClone();

Accessing length of nested property [duplicate]

This question already has answers here:
Length of a JavaScript object
(43 answers)
Closed 2 years ago.
I have this data and would like to access the length (i.e. number) of procedures nested within 'procedures'. How might I do this?
let specialties = [
{
name: 'Emergency)',
number: '1',
procedures: {
procedure1: 'PoPo',
procedure2: 'Lala',
procedure3: 'Dipsy',
}]
You can count the keys of the object:
let specialties = [
{
name: 'Emergency)',
number: '1',
procedures: {
procedure1: 'PoPo',
procedure2: 'Lala',
procedure3: 'Dipsy',
}
}
]
console.log(Object.keys(specialties[0].procedures).length)

Javascript - To remove duplicates in array object for ES5 [duplicate]

This question already has answers here:
How to call reduce on an array of objects to sum their properties?
(23 answers)
Get all unique values in a JavaScript array (remove duplicates)
(91 answers)
Closed 3 years ago.
I have the an array, listPeople and to eliminate duplicates I use the following code in ES6:
var listPeople = [{
"nomCustomer": "Fran",
"attrName": "aaa"
},
{
"nomCustomer": "John",
"attrName": "ccc"
},
{
"nomCustomer": "Paul",
"attrName": "ddd"
},
{
"nomCustomer": "Paul",
"attrName": "ddd"
}
];
var list = listPeople.reduce((unique, o) => {
if (!unique.some(obj => obj.nomCustomer === o.nomCustomer && obj.attrName === o.attrName)) {
unique.push(o);
}
return unique;
}, []);
console.log(list)
What would it be like for ES5?

Categories