1

I need help combining two objects with same keys into one object. I am getting these objects from an api request.

My first object:

var obj1 = {
RefinableString15: "Claims",
RefinableString16: "2123",
RefinableString17: "New York Cyberstate",
RefinableString19: "Global Property",
RefinableString20: "",
RefinableString21: "",
RefinableString22: "",
RefinableString23: "",
RefinableString24: "",
RefinableString25: ""
}

My second object:

var obj2 = {
RefinableString15: "",
RefinableString16: "",
RefinableString17: "",
RefinableString19: "",
RefinableString20: "Test",
RefinableString21: "Red",
RefinableString22: "Test",
RefinableString23: "Red",
RefinableString24: "Test",
RefinableString25: "Green"
}

I want it to look like this in the end:

{
RefinableString15: "Claims",
RefinableString16: "2123",
RefinableString17: "New York Cyberstate",
RefinableString19: "Global Property",
RefinableString20: "Test",
RefinableString21: "Red",
RefinableString22: "Test",
RefinableString23: "Red",
RefinableString24: "Test",
RefinableString25: "Green"
}

I have tried Object.assign, but it didn't work for my problem.

3
  • 3
    You need to delete the empty fields of the second object before using assign() or they will overwrite the first object's non-empty fields. Commented Mar 10, 2020 at 11:29
  • I can't, all properties are "read only property" so it wont let me delete. any other way with out using "Object.assign". Commented Mar 10, 2020 at 11:31
  • 1
    @SaadRH then there's something you're not telling us. Typescript has read-only properties (you didn't tag TS), and Javascript lets you create non-writable properties with Object.defineProperty, but that wouldn't apply to something you got from an API call. What's your real problem here? Commented Mar 10, 2020 at 11:34

4 Answers 4

2

var obj1 = {
  RefinableString15: "Claims",
  RefinableString16: "2123",
  RefinableString17: "New York Cyberstate",
  RefinableString19: "Global Property",
  RefinableString20: "",
  RefinableString21: "",
  RefinableString22: "",
  RefinableString23: "",
  RefinableString24: "",
  RefinableString25: ""
}

var obj2 = {
  RefinableString15: "",
  RefinableString16: "",
  RefinableString17: "",
  RefinableString19: "",
  RefinableString20: "Test",
  RefinableString21: "Red",
  RefinableString22: "Test",
  RefinableString23: "Red",
  RefinableString24: "Test",
  RefinableString25: "Green"
}

function merge(object1, object2) {
  const keys = Object.keys(object1);
  return keys.reduce((result, item) => {
    result[item] = object1[item] || object2[item];
    return result;
  }, {})
}

console.log(merge(obj1, obj2))

Sign up to request clarification or add additional context in comments.

3 Comments

What happens if neither value is falsey but they're different? What happens if the set of keys isn't exactly the same? What happens if the first value is falsey and the key doesn't exist in the second object? You'll get undefined instead of an empty string and now you've broken any code that relies on the type being string.
@JaredSmith as per the OP, the keys are always the same. As for what if neither key is falsey, it's up to the person called the function which one they want to give priority to
Fair enough. Have plus one.
2

I have done this:

var obj1 = 
    { RefinableString15: "Claims"
    , RefinableString16: "2123"
    , RefinableString17: "New York Cyberstate"
    , RefinableString19: "Global Property"
    , RefinableString20: ""
    , RefinableString21: ""
    , RefinableString22: ""
    , RefinableString23: ""
    , RefinableString24: ""
    , RefinableString25: ""
    } 
var obj2 = 
    { RefinableString15: ""
    , RefinableString16: ""
    , RefinableString17: ""
    , RefinableString19: ""
    , RefinableString20: "Test"
    , RefinableString21: "Red"
    , RefinableString22: "Test"
    , RefinableString23: "Red"
    , RefinableString24: "Test"
    , RefinableString25: "Green"
    } 

    
const cleanObj = o => Object.entries(o).reduce((a,[k,v])=>{if(v!='') a[k]=v;return a}, {}) 
     
var obj3 = { ...obj1, ...cleanObj(obj2)  }

console.log ( obj3 )
.as-console-wrapper { max-height: 100% !important;  top: 0; }

Comments

1

You need to clean up empty properties and merge two objects. Something like that

  function clean(obj) {
            for (const propName in obj) {
                if (
                    obj[propName] === '' ||
                    obj[propName] === null ||
                    obj[propName] === undefined
                ) {
                    delete obj[propName];
                }
            }
            return obj;
        }
    const result = {...clean(obj1),...clean(obj2)};

Comments

1

Use forEach loop and build one object where values push only non empty strings.

var obj1 = {
  RefinableString15: "Claims",
  RefinableString16: "2123",
  RefinableString17: "New York Cyberstate",
  RefinableString19: "Global Property",
  RefinableString20: "",
  RefinableString21: "",
  RefinableString22: "",
  RefinableString23: "",
  RefinableString24: "",
  RefinableString25: ""
};

var obj2 = {
  RefinableString15: "",
  RefinableString16: "",
  RefinableString17: "",
  RefinableString19: "",
  RefinableString20: "Test",
  RefinableString21: "Red",
  RefinableString22: "Test",
  RefinableString23: "Red",
  RefinableString24: "Test",
  RefinableString25: "Green"
};

const res = {};
Object.keys(obj1).forEach(key => (res[key] = obj1[key] || obj2[key]));

console.log(res);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.