1

I'm aware this muct a very common question, however I tried sources and most of them provide answers with some libraries, ES6 or some methods that native js does not support. I want to purely replace one object with another based on a condition- no ES6, no clone and no copy.

in the below case: i want to get the value from subObj and replace them with the values in Obj1

ex: key attributes "key2" and "key3" to be replaced from the values present in the subObj

Obj1 = {
"key1" : {values:[1, 2, 44, 505]},
"key2" : {values:[91, 25, 44, 5995]},
"key3" : {values:[1, 24, 44, 595]},
"key4" : {values:[17, 28, 44, 559]}
}

subObj = {

**"key2" : {values:[3, 3, 3, 444]},** // add this one 
**"key3" : {values:[1, 0, 0, 0]}**

}

so the output looks like:

Obj1 = {
"key1" : {values:[1, 2, 44, 505]},
**"key2" :{ values:[3, 3, 3, 444]},**
**"key3" : {values:[1, 0, 0, 0]},**
"key4" : {values:[17, 28, 44, 559]}
}

so far i have tried this:

  somefunc: function(condition) {

          if (condition) {
            Object.keys(Obj1).forEach(function(key) { Obj[key] = Obj1[key]});
            return Obj1;
          }
          return Obj;
        },

this does not work as expected, any help here? thx

7
  • @RokoC.Buljan I'm guessing bold tags, OP, what do you mean by o/p? Your question is unclear to me. Commented Aug 25, 2020 at 23:29
  • @all: Please correct if im worng, isnt object.assign and spread operator all ES6 notations ? as I mentioned above I Cannot use ES6, so these wont work right? Commented Aug 25, 2020 at 23:36
  • "ES6 or some methods that native js does not support" - I'm wondering if there's some confusion here as to what ES6 actually is? ES6, also known as ES2015, was released a full 5 years ago and is natively supported in all modern browsers (plus all recent versions of NodeJS etc). Some features of more recent versions of JavaScript (ES2016..ES2020) are sometimes lumped in with ES6 - in this broader usage, it basically just means "modern JavaScript". Generally, you shouldn't worry about which ES version you're using, as long as you're transpiling with something like Babel. Commented Aug 25, 2020 at 23:37
  • we're not using babel, so this wont work, our framework is EXTJs old version and currently no support for ES6, this is not due to browser support, our app fremawork does not supoort Commented Aug 25, 2020 at 23:38
  • 1
    @user1234 Fair enough (never heard of EXTJs, I assume it's something legacy-ish as the Wikipedia page mentions "DHTML"?). Adding Babel into your build tool chain would require a little dev work up front but might end up saving you a lot of headaches later on, assuming you're building something substantial. Commented Aug 25, 2020 at 23:42

3 Answers 3

3
function merge(Obj1, subObj) {
    const result = {};

    Object.keys(Obj1).forEach((key) => {
        result[key] = Obj1[key]
    })

    Object.keys(subObj).forEach((k) => {
        if(result[k]){
            console.log('update')
            result[k] = subObj[k]
        } else {
            console.log("add")
            result[k] = subObj[k]
        }
    })

    console.log(result)
    return result
}

merge(Obj1, subObj)

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

3 Comments

cannot be Es6 sorry..:(
How bout creating a function that accepts both objects.
Editing the answer basic function declaration that accepts both objects as parameters. Populates and then cross-references.
2

You can use Object.assign().

// ...

if (condition) {
    return Object.assign(Obj1, subObj);
}

// ...

1 Comment

isnt Object.assign ES6?
1

This is what we use the spread operator for

const Obj1 = {
"key1" : {values:[1, 2, 44, 505]},
"key2" : {values:[91, 25, 44, 5995]},
"key3" : {values:[1, 24, 44, 595]},
"key4" : {values:[17, 28, 44, 559]}
}

const subObj = {

"key2" : {values:[3, 3, 3, 444]},
"key3" : {values:[1, 0, 0, 0]}

}

console.log({ ...Obj1, ...subObj });

1 Comment

cannot be Es6 sorry..:(

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.