3

I tried to merge using this way but it making two different keys of meta_information and meta_information.image

merchant = Object.assign(merchant, media_mode)

I have tried this way but its not working

var media_mode = {
    "featured_media_square" : "square",
    "featured_media_landscape" :"landscape",
    "logo" : "a.png",
    "meta_information.image" : "b.png"  
}

var merchant = {meta_information : {image : ""}}

I want to get result like this

merchant = {
    "featured_media_square" : "square",
     "featured_media_landscape" : "landscape",
     "logo" : "a.png",
     "meta_information" : {"image" : "b.png"}  
}
3
  • @ngfelixl its not working , its not giving the response i mentioned above Commented Jan 23, 2019 at 13:34
  • 2
    You're getting a lot of unhelpful answers. I would recommend first modifying your first object to match the format of the second before performing your nested merge. I.e. transform the dot notation in the keys to proper nested objects. Then you can use existing strategies for a deep merge. Commented Jan 23, 2019 at 13:41
  • 1
    for some reason Iam getting downwoted, yet its the only answer that produces the model you asked for, before it gets lots, you cant merge them, you have to remap the value. "meta_information.image" cant be easily transformed to a object path Commented Jan 23, 2019 at 13:46

7 Answers 7

4

var media_mode = {
  "featured_media_square" : "square",
  "featured_media_landscape":"landscape",
  "logo":"a.png",
  "meta_information.image":"b.png"  
}

var merchant = {meta_information:{image:""}};
  
for (var key in media_mode) {
  	  var kn = key.split(".");
  	  for (var key2 in merchant) {
    if (kn[0] === key2) {
    	var tmp = media_mode[key];
        media_mode[kn[0]] = {};
    	media_mode[kn[0]][kn[1]] = tmp;
        delete media_mode[key]
    }
  }
}
  
console.log(media_mode);
ECMA5 version.

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

Comments

3

They cant merge, you would have to remap them manually, for instance desctructure media_mode, and pull "image" out of it and then having rest of the object as "...rest", then just put that together in the exact model you asked for

var media_mode = {
  featured_media_square: "square",
  featured_media_landscape: "landscape",
  logo: "a.png",
  "meta_information.image": "b.png",
};

var { "meta_information.image": image, ...rest } = media_mode;

merchant = {
  ...rest,
  meta_information: { image },
};

console.log(merchant);

ES5 as requested

var media_mode = {
  featured_media_square: "square",
  featured_media_landscape: "landscape",
  logo: "a.png",
  "meta_information.image": "b.png",
};

const image = media_mode["meta_information.image"];

var merchant = Object.assign({}, media_mode, {
  meta_information: { image: image },
});

console.log(merchant);

note that in this solution key "meta_information.image" remains in the result, if you would insists on removing it, just run

delete merchant["meta_information.image"]

2 Comments

can you tell me how to do it in es5 syntax
@YashGandhi there you go ES5, not as elegant but should work
1

I tried to merge using this way but it making two different keys of meta_information and meta_information.image

Because they are two different keys.

meta_information and meta_information.image won't merge/override unless the keys match with ===.

What you're asking is not available right out of the box in JavaScript.

Comments

1

var media_mode = {
  "featured_media_square" : "square",
  "featured_media_landscape":"landscape",
  "logo":"a.png",
  "meta_information.image":"b.png"  
}

var merchant = {meta_information:{image:""}};
 
var merged = {...media_mode, ...merchant};

console.log(merged);

works exactly as you expect.

Comments

0

You have Object like this
merchant= {
"featured_media_square" : "square",
"featured_media_landscape":"landscape",
"logo":"a.png",
"meta_information": {"image":"b.png"}
}
You can do something like to merge .
const newObj= {
...merchant,
"meta_information": {"image" : "b.png"}
}

Comments

0

You can't merge meta_information.image with a nested object with the structure meta_information: { image: '' }. JavaScript cannot do that natively - you'd have to do it programmatically. If you want to merge them with using an ES5 method, the easiest method is to use Object.assign like you attempted, then simply delete the key you don't want.

const media_mode = {
  featured_media_square: 'square',
  featured_media_landscape: 'landscape',
  logo: 'a.png',
  'meta_information.image': 'b.png'
}

const merchant = {
  meta_information: { image: 'b.png' }
}

Object.assign(merchant, media_mode);
delete merchant['meta_information.image'];
console.log(merchant);

3 Comments

where is image value?
It's there now.
you mutated the object pls try it with the object i provided
-2

We can use Spread Operator

   var media_mode = {
        "featured_media_square" : "square",
        "featured_media_landscape":"landscape",
        "logo":"a.png",
        "meta_information.image":"b.png"  
    }

    var merchant = {meta_information:{image:""}};
    
    var merged = {...media_mode,...merchant};
    
    console.log(merged)

1 Comment

Spread Operator only does the first level. Anything after that is NOT merged because.... javascript.

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.