0

我的服务器有一个差异对象,需要将其合并到本地表示中。所以我需要一种方法来在原始表示中添加当前未定义的对象部分。

例子:

// The serverresponse
{
  "PRIVATE": {
      "SCREENS": {
          "WORKSPACE": {
              "POSTS": {
                  "1363391397125": {
                      "TGT": "1363390641817",
                      "ACN": "position",
                      "TO": [
                          57.1875,
                          69.21182266009852
                      ]
                  }
              }
          }
      }
  }
}

// This part is similar to my local representation
{
  "PRIVATE": {
      "SCREENS": {
          "WORKSPACE": {
              "POSTS": {}
           }
       }
}


// So I want to insert this part of the given data at same position inside the local data
{
  "1363391397125": {
                    "TGT": "1363390641817",
                    "ACN": "position",
                    "TO": [
                          57.1875,
                          69.21182266009852
                      ]
                  }
}

有很多不同的部分要插入,所以我正在寻找一种替代方案来迭代我将拥有的每种情况。

4

1 回答 1

3

看到您添加了 jQuery 标签,您可以使用 jQuery 的extend()方法,类似于以下内容:

// The serverresponse
var x = {
    "PRIVATE": {
        "SCREENS": {
            "WORKSPACE": {
                "POSTS": {
                    "1363391397125": {
                        "TGT": "1363390641817",
                            "ACN": "position",
                            "TO": [
                        57.1875,
                        69.21182266009852]
                    }
                }
            }
        }
    }
};

// This part is similar to my local representation
var y = {
    "PRIVATE": {
        "SCREENS": {
            "WORKSPACE": {
                "POSTS": {}
            }
        }
    }
};

// merges x into y
$.extend(y, x); 

// use $.extend(x, y); if you intended it to be the other way around.

DEMO -$.extend()用于合并对象。


于 2013-03-16T00:13:11.530 回答