Reputation: 140
I have 2 objects like this
{
"2-14": "text-body",
"3-28": "h1",
"3-30": "h2",
"3-36": "color-primary",
"3-38": "color-secondary",
"3-40": "color-black-100",
"3-43": "color-black-50"
}
and
{
"3-36": "#4992FF",
"3-38": "#49DEFF",
"3-40": "#000000",
"3-43": "#808080"
}
I need to merge these two like below
{
"color-primary": "#4992FF",
"color-secondary": "#49DEFF",
"color-black-100": "#000000",
"color-black-50": "#808080",
}
Basically, Value of the first object will be key and the value of second object will become value of new object. I need to merge where key matches. Please help.
I tried this way to merge both. But not able to get new object as required.
const newArray = {};
for (const key of Object.getOwnPropertyNames(array1)) {
newArray[key] = array2[key];
}
this gives me
{
"3-36": "color-primary",
"3-38": "color-secondary",
"3-40": "color-black-100",
"3-43": "color-black-50"
}
Upvotes: 0
Views: 57
Reputation: 6019
this is my try;
const obj1 = {
"2-14": "text-body",
"3-28": "h1",
"3-30": "h2",
"3-36": "color-primary",
"3-38": "color-secondary",
"3-40": "color-black-100",
"3-43": "color-black-50"
}
const obj2= {
"3-36": "#4992FF",
"3-38": "#49DEFF",
"3-40": "#000000",
"3-43": "#808080"
}
const objFinal = {};
Object.keys(obj1).forEach(k => {
if (obj2[k]){
objFinal[obj1[k]]=obj2[k];
}
});
console.log(objFinal);
Upvotes: 1
Reputation: 1639
const names = {
"2-14": "text-body",
"3-28": "h1",
"3-30": "h2",
"3-36": "color-primary",
"3-38": "color-secondary",
"3-40": "color-black-100",
"3-43": "color-black-50"
};
const colors = {
"3-36": "#4992FF",
"3-38": "#49DEFF",
"3-40": "#000000",
"3-43": "#808080"
};
// find all keys that are present in both object
const keysToMerge = [];
Object.keys(names).forEach(key => Boolean(colors[key]) && keysToMerge.push(key));
// merge them
const result = {};
keysToMerge.forEach(key => result[names[key]] = colors[key]);
Upvotes: 0
Reputation: 181
First a short remark: You are not dealing with Arrays here, but Objects.
Here is a pretty basic solution that should give you what you are loooking for:
const obj1 = {
"2-14": "text-body",
"3-28": "h1",
"3-30": "h2",
"3-36": "color-primary",
"3-38": "color-secondary",
"3-40": "color-black-100",
"3-43": "color-black-50"
};
const obj2 = {
"3-36": "#4992FF",
"3-38": "#49DEFF",
"3-40": "#000000",
"3-43": "#808080"
};
const result = { };
Object.keys(obj1).forEach(key => {
if (obj2[key]) {
result[obj1[key]] = obj2[key];
}
});
The result will be:
{
"color-primary": "#4992FF",
"color-secondary": "#49DEFF",
"color-black-100": "#000000",
"color-black-50": "#808080"
}
Upvotes: 1
Reputation: 118
Something like this?
const names = {
"2-14": "text-body",
"3-28": "h1",
"3-30": "h2",
"3-36": "color-primary",
"3-38": "color-secondary",
"3-40": "color-black-100",
"3-43": "color-black-50"
}
const colors = {
"3-36": "#4992FF",
"3-38": "#49DEFF",
"3-40": "#000000",
"3-43": "#808080"
}
const result = {};
Object.entries(colors).forEach(color => {
const [key, value] = color;
if(key in names) {
result[names[key]] = value;
}
})
/*
{
"color-primary": "#4992FF",
"color-secondary": "#49DEFF",
"color-black-100": "#000000",
"color-black-50": "#808080",
}
*/
Upvotes: 1