简体   繁体   English

将属性从一个对象添加到另一个

[英]adding properties from one object to another

I have two objects with a similar structure - 我有两个结构相似的对象-

    var locations = { 
        "regions": [{
        "name": "Asia Pacific",
        "handle": "asia-pacific",
        "iso_code": "",
        "iso_code_3": "",
        "countries": [{
            "name": "Republic of Korea",
            "iso_code": "kr",
            "iso_code_3": "kor"
          },{
            "name": "Japan",
            "iso_code": "jp",
            "iso_code_3": "jap"
          }
        ]
      }]
    }


    var additionalLocations = { 
        "regions": [{
        "name": "Asia Pacific",
        "handle": "asia-pacific",
        "iso_code": "",
        "iso_code_3": "",
        "countries": [{
            "name": "Malaysia",
            "iso_code": "my",
            "iso_code_3": "mys",
          }
        ]
      }]
    }

How can I the country, Malaysia, from the additionalLocations object to the locations object? 我该如何将马来西亚(马来西亚)从AdditionalLocations对象更改为locations对象?

It should look like this - 它看起来应该像这样-

   var locations = { 
        "regions": [{
        "name": "Asia Pacific",
        "handle": "asia-pacific",
        "iso_code": "",
        "iso_code_3": "",
        "countries": [{
            "name": "Republic of Korea",
            "iso_code": "kr",
            "iso_code_3": "kor"
          },{
            "name": "Japan",
            "iso_code": "jp",
            "iso_code_3": "jap"
          },{
            "name": "Malaysia",
            "iso_code": "my",
            "iso_code_3": "mys",
          }
        ]
      }]
    }

I'm hoping there's a nifty underscore method that will do it for me. 我希望有一个漂亮的下划线方法可以为我做这件事。

locations.regions[0].countries.push(additionalLocations.regions[0].countries[0])

如果要在一个循环中的多个位置执行此操作,则需要更多操作,但对于您发布的情况,此操作应该可以。

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

相关问题 将对象属性从一个JSON数据文件添加到另一个 - Adding object properties from one JSON data file to another 使用条件将属性从一个对象复制到另一个对象 - Copying properties from one object to another with a condition 将对象属性从一个数组映射到另一个数组 - Mapping object properties from one array to another 更新 object 数组而不添加来自另一个 object 的新属性 - Update object array without adding new properties from another object 根据另一个属性的值将属性从一个对象传输到另一个对象 - Transferring Properties from one to another object based on the value of another property 在另一个对象中使用一个对象的功能和属性 - Use functions and properties from one object in another object 用一个对象的属性替换一个对象的属性? - Replace one object's properties with the properties of another? 如何将属性从一个 object 复制到另一个 typescript - How to copy properties from one object to another in typescript 创建 Function 将属性从一个 Object 复制到另一个 - Create Function to Copy Properties From One Object to Another 如何在不覆盖Javascript的情况下将一个对象的属性添加到另一个对象中 - How to add properties from one object into another without overwriting in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM