两个如何将2个json对象合并为一个 [英] How two merge 2 json objects into one

查看:1380
本文介绍了两个如何将2个json对象合并为一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于编辑提供者记录的表格.提供者记录与多个地址有关联,因此提供者可以具有家庭,办公室,休假等地址类型.

I have a form for editing a provider record. The provider record has a relationship with multiple address, so a provider can has home, office, vacation, etc address types.

来自服务器的json地址对象只是带来了提供者拥有的地址,因此,如果提供者只有假期"类型的地址,那么这是唯一出现的记录,但是我必须具有编辑形式所有地址类型,以便编辑现有地址或添加新地址.

The json addresses object that comes from server just brings the addresses that the provider has, so if the provider has only a "vacation" type address then that's the only record that comes in but in the editing form I must have to have all the addresses types in order to edit the existing ones or add a new one.

我不知道我的方法是否很好,但是在我的表单中有一个"jsonBaseAddresses",其中包含所有可能的地址类型,并且该对象使用Knockout绑定到了表单(这实际上可行),所以我想合并带有"jsonBaseAddresses"的"jsonRemoteAddress",以便具有"jsonBaseAddresses"但与远程json对象中的数据合并.

I don't know if my approach is fine but I have in my form a "jsonBaseAddresses" that contains all the address possible types and this object is binded using Knockout to the form (this actually works) so I want to merge the "jsonRemoteAddress" with "jsonBaseAddresses" in order to have the "jsonBaseAddresses" but merged with the data that comes in the remote json object.

再次,我不知道这是否是正确的方法.感谢任何帮助.这是我想做的jsFiddle示例.

Again, i dont know if this is the right way to do it. Appreciate any help. Here is a jsFiddle sample of what i want to do.

http://jsfiddle.net/HJrGf/4/

谢谢

推荐答案

为什么不只遍历远程地址,然后将其推送到视图模型中的地址,如?

Why don't you just iterate over the remote addresses and push them to addresses in the view model like ?

var vm = new App(jsonBaseAddresses );
for ( var i = 0; i < jsonRemoteAddresses.length; i++){
vm.addresses.push(new Address(jsonRemoteAddresses[i]);
}

依此类推...

要合并地址,可以使用以下解决方案:

To merge addresses you can use following solutions:

  1. 您可以将属性像等于一样引入到地址并比较字段 标识唯一地址的地址

  1. You can introduce property to Address like Equals and compare fields that identify the unique address

选择本文中的任何实现如何确定两个JavaScript对象是否相等? 获取地址哈希码,并在上面的循环中比较哈希 要合并的地址的代码.我在其中一个使用了此解决方案 项目.还要检查 http://documentcloud.github.com/underscore/#isEqual

Select any implementation in this post How to determine equality for two JavaScript objects? to get the address hash code and in the loop above compare the hash codes of the addresses to merge. I used this solution in one of my projects. And also check this http://documentcloud.github.com/underscore/#isEqual

这篇关于两个如何将2个json对象合并为一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆