Daft
Daft

Reputation: 10964

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

Upvotes: 3

Views: 1140

Answers (3)

Nina Scholz
Nina Scholz

Reputation: 386550

You could iterate the keys and replace the properties.

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' },
    obj2 = { API_NAME__c: 'Name', API_ADDRESS__c: 'Address', API_DOB__c: 'Date of birth' };
  
Object.keys(obj2).forEach(function (k) {
    obj1[obj2[k]] = obj1[k];
    delete obj1[k];
});

console.log(obj1);

As an alternative, you could generate a new object.

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' },
    obj2 = { API_NAME__c: 'Name', API_ADDRESS__c: 'Address', API_DOB__c: 'Date of birth' },
    obj3 = Object.assign(...Object.keys(obj1).map(k => ({ [obj2[k] || k]: obj1[k] })));

console.log(obj3);

The third object ...

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' },
    obj2 = { API_NAME__c: 'Name', API_ADDRESS__c: 'Address', API_DOB__c: 'Date of birth' },
    obj3 = {};

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

console.log(obj1);
console.log(obj3);

Upvotes: 1

cнŝdk
cнŝdk

Reputation: 32145

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

Upvotes: 2

tymeJV
tymeJV

Reputation: 104775

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/

Upvotes: 1

Related Questions