Redefine object properties keys starting with prefix [duplicate] - javascript

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)

Related

how to push fields transformed to an array of object in javascript [duplicate]

This question already has answers here:
Variable as the property name in a JavaScript object literal? [duplicate]
(3 answers)
How to get a subset of a javascript object's properties
(36 answers)
Closed 6 months ago.
i have the following codes in javascript, what i am trying to achieve is to remove name and gender from data object and replace it with the field combined_fields: [{name: "james", gender: "M"}]
const data = {
name: "james",
age: 12,
gender: "M",
country: "US",
}
const getData = (data) => {
const combinedFieldsResult = [];
for (const [key, value] of Object.entries(data)) {
if (key === "name" || key === "gender") {
combinedFieldsResult.push({key: name}); <==== something is wrong here
}
}
return combinedFieldsResult;
};
const result = getData(data)
console.log(result);
the final result should be:
const data = {
age: 12,
combined_fields: [{name: "james", gender: "M"}],
country: "US",
}
how can i possibly achieve the above result?

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' }))
})

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

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.

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