Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare keys from 2 objects and replace

I have 2 objects, one contains the data I want, but I'm also getting some unwanted API names, I have a second object which has the correct names with which to replace the these API names.

But I'm having a hard time swapping them out, I'm unsure how to compare the object keys and swap them out.

obj1 = {
        Id: 'XXX123CCCVVV',
        API_NAME__c : 'Joe',
        API_ADDRESS__c : '123 FAKE STREET',
        API_DOB__c : '6/6/1966',
        Account : 'Johnson & Smith',
        Position : 'CEO' 
    }

obj2 = {
        API_NAME__c : 'Name',
        API_ADDRESS__c : 'Address',
        API_DOB__c : 'Date of birth'
    }

I would like to compare the keys of these 2 objects, and anywhere they match, swap the value of obj2 for the key of obj1, so obj1 would end up as follows:

obj1 = {
        Id: 'XXX123CCCVVV',
        Name : 'Joe',
        Address : '123 FAKE STREET',
        Date of birth : '6/6/1966',
        Account : 'Johnson & Smith',
        Position : 'CEO' 
    }

Can anyone suggest a way to accomplish this?

I'm using underscore.js at the moment if that could be of assistance.


UPDATE

I really appreciate the excellent answers below, but I wonder would it be at all possible to extend this logic to produce a third object containing only the values with the new property names?

So that I'd end up with the following:

obj1 = {
        Id: 'XXX123CCCVVV',
        Account : 'Johnson & Smith',
        Position : 'CEO' 
    }

obj3 = {
        Name : 'Joe',
        Address : '123 FAKE STREET',
        Date of birth : '6/6/1966',
    }
like image 360
Daft Avatar asked Nov 30 '25 16:11

Daft


2 Answers

Well simply loop over the keys in obj2 and update obj1 if the iterated key exists on it with the values from obj2 and remove the old value of this key:

Object.keys(obj2).forEach(function(key){
    if(obj1[key]){
        obj1[obj2[key]] = obj1[key];
        delete obj1[key];
    }
});

Demo:

var obj1 = {
  Id: 'XXX123CCCVVV',
  API_NAME__c: 'Joe',
  API_ADDRESS__c: '123 FAKE STREET',
  API_DOB__c: '6/6/1966',
  Account: 'Johnson & Smith',
  Position: 'CEO'
}

var obj2 = {
  API_NAME__c: 'Name',
  API_ADDRESS__c: 'Address',
  API_DOB__c: 'Date of birth'
}

Object.keys(obj2).forEach(function(key){
    if(obj1[key]){
        obj1[obj2[key]] = obj1[key];
        delete obj1[key];
    }
});
console.log(obj1);
like image 175
cнŝdk Avatar answered Dec 02 '25 06:12

cнŝdk


Interesting problem, you can loop the keys of obj1, if obj2 contains that key, replace the key in obj1 with that value in obj2:

for (let key in obj1) {
    if (obj2.hasOwnProperty(key)) {
        obj1[obj2[key]] = obj1[key]
        delete obj1[key]; //remove old entry
    }
}

Or with .reduce

let newObj = Object.keys(obj1).reduce((o, k) => {
    let newKey = obj2.hasOwnProperty(k) ? obj2[k] : k;
    o[newKey] = obj1[k];

    return o;
}, {});

Demo: https://jsfiddle.net/f3vuoppf/2/

like image 21
tymeJV Avatar answered Dec 02 '25 04:12

tymeJV