简体   繁体   English

如何在javascript中合并两个嵌套对象

[英]How to merge two nested objects in javascript

I tried to merge using this way but it making two different keys of meta_information and meta_information.image 我尝试使用这种方式合并,但它创建了两个不同的meta_informationmeta_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"}  
}

 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. ECMA5版本。

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 他们不能合并,你必须手动重新映射它们,例如desctructure media_mode,并从中拉出“image”,然后将对象的其余部分称为“... rest”,然后将它们放在精确的模型中要求

 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 ES5按要求提供

 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 请注意,在此解决方案中,键“meta_information.image”仍保留在结果中,如果您坚持要删除它,只需运行

delete merchant["meta_information.image"]

I tried to merge using this way but it making two different keys of meta_information and meta_information.image 我尝试使用这种方式合并,但它创建了两个不同的meta_information和meta_information.image键

Because they are two different keys. 因为它们两个不同的键。

meta_information and meta_information.image won't merge/override unless the keys match with === . 除非键与===匹配,否则meta_informationmeta_information.image将不会合并/覆盖。

What you're asking is not available right out of the box in JavaScript. 在JavaScript中,您提供的内容无法直接使用。

 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. 完全按照您的预期工作。

You have Object like this 你有像这样的对象
merchant= { 商人= {
"featured_media_square" : "square", “featured_media_square”:“square”,
"featured_media_landscape":"landscape", “featured_media_landscape”: “风景”,
"logo":"a.png", “标识”:“a.png”
"meta_information": {"image":"b.png"} “meta_information”:{“image”:“b.png”}
} }
You can do something like to merge . 您可以执行类似合并的操作。
const newObj= { const newObj = {
...merchant, ...商人,
"meta_information": {"image" : "b.png"} “meta_information”:{“image”:“b.png”}
} }

You can't merge meta_information.image with a nested object with the structure meta_information: { image: '' } . 您无法将meta_information.image与具有结构meta_information: { image: '' }的嵌套对象合并meta_information: { image: '' } JavaScript cannot do that natively - you'd have to do it programmatically. JavaScript本身无法做到这一点 - 您必须以编程方式执行此操作。 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. 如果要使用ES5方法将它们合并,最简单的方法是使用像您尝试的Object.assign ,然后只需删除您不想要的密钥。

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

We can use Spread Operator 我们可以使用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) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM