在更新jsons时比较2个jsons和更改键值

时间:2015-03-12 05:39:18

标签: javascript jquery arrays json

有2个jsons 我添加了json代码



var json1 = [
  {
    "name": "padd",
    "value": "1",
    "parentName": "",
    "parentValue": "",
    "children": [],
    "canDisplay": true,
    "showRecord": true
  },
  {
    "name": "padd",
    "value": "2",
    "parentName": "",
    "parentValue": "",
    "children": [],
    "canDisplay": true,
    "showRecord": true
  },
  {
    "name": "padd",
    "value": "3",
    "parentName": "",
    "parentValue": "",
    "children": [],
    "canDisplay": true,
    "showRecord": true
  },
  {
    "name": "padd",
    "value": "4",
    "parentName": "",
    "parentValue": "",
    "children": [],
    "canDisplay": true,
    "showRecord": true
  },
  {
    "name": "padd",
    "value": "5",
    "parentName": "",
    "parentValue": "",
    "children": [],
    "canDisplay": true,
    "showRecord": true
  },
  {
    "name": "padd",
    "value": "Unknown",
    "parentName": "",
    "parentValue": "",
    "children": [],
    "canDisplay": true,
    "showRecord": true
  }
];






json2=[
  {
    "name": "padd",
    "value": "1",
    "parentName": "",
    "parentValue": "",
    "children": [],
    "canDisplay": false,
    "showRecord": true
  },
  {
    "name": "padd",
    "value": "2",
    "parentName": "",
    "parentValue": "",
    "children": [],
    "canDisplay": false,
    "showRecord": true
  },
  {
    "name": "padd",
    "value": "3",
    "parentName": "",
    "parentValue": "",
    "children": [],
    "canDisplay": false,
    "showRecord": true
  },
  {
    "name": "padd",
    "value": "4",
    "parentName": "",
    "parentValue": "",
    "children": [],
    "canDisplay": false,
    "showRecord": true
  },
  {
    "name": "padd",
    "value": "5",
    "parentName": "",
    "parentValue": "",
    "children": [],
    "canDisplay": true,
    "showRecord": true
  },
  {
    "name": "padd",
    "value": "Unknown",
    "parentName": "",
    "parentValue": "",
    "children": [
      {
        "name": "terminal_state",
        "value": "00",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": false,
        "showRecord": true,
        "$$hashKey": "object:31491"
      },
      {
        "name": "terminal_state",
        "value": "AL",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": false,
        "showRecord": true,
        "$$hashKey": "object:31492"
      },
      {
        "name": "terminal_state",
        "value": "AR",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": false,
        "showRecord": true,
        "$$hashKey": "object:31493"
      },
      {
        "name": "terminal_state",
        "value": "AZ",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31494"
      },
      {
        "name": "terminal_state",
        "value": "CA",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31495"
      },
      {
        "name": "terminal_state",
        "value": "CO",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31496"
      },
      {
        "name": "terminal_state",
        "value": "CT",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31497"
      },
      {
        "name": "terminal_state",
        "value": "FL",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31498"
      },
      {
        "name": "terminal_state",
        "value": "GA",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31499"
      },
      {
        "name": "terminal_state",
        "value": "IA",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31500"
      },
      {
        "name": "terminal_state",
        "value": "ID",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31501"
      },
      {
        "name": "terminal_state",
        "value": "IL",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31502"
      },
      {
        "name": "terminal_state",
        "value": "IN",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31503"
      },
      {
        "name": "terminal_state",
        "value": "KS",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31504"
      },
      {
        "name": "terminal_state",
        "value": "KY",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31505"
      },
      {
        "name": "terminal_state",
        "value": "LA",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31506"
      },
      {
        "name": "terminal_state",
        "value": "MA",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31507"
      },
      {
        "name": "terminal_state",
        "value": "MD",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31508"
      },
      {
        "name": "terminal_state",
        "value": "MI",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31509"
      },
      {
        "name": "terminal_state",
        "value": "MN",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31510"
      },
      {
        "name": "terminal_state",
        "value": "MO",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31511"
      },
      {
        "name": "terminal_state",
        "value": "MS",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31512"
      },
      {
        "name": "terminal_state",
        "value": "MT",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31513"
      },
      {
        "name": "terminal_state",
        "value": "NC",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31514"
      },
      {
        "name": "terminal_state",
        "value": "ND",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31515"
      },
      {
        "name": "terminal_state",
        "value": "NE",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31516"
      },
      {
        "name": "terminal_state",
        "value": "NJ",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31517"
      },
      {
        "name": "terminal_state",
        "value": "NM",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [
          {
            "name": "terminal_city",
            "value": "Albuquerque",
            "parentName": "terminal_state",
            "parentValue": "NM",
            "children": [
              {
                "name": "enterminalname",
                "value": "NM Albuquerque - Nustar - 4253",
                "parentName": "terminal_city",
                "parentValue": "Albuquerque",
                "children": [],
                "canDisplay": false,
                "showRecord": true,
                "$$hashKey": "object:31716"
              },
              {
                "name": "enterminalname",
                "value": "NM Albuquerque - P66 - 4254",
                "parentName": "terminal_city",
                "parentValue": "Albuquerque",
                "children": [],
                "canDisplay": false,
                "showRecord": true,
                "$$hashKey": "object:31717"
              },
              {
                "name": "enterminalname",
                "value": "NM Albuquerque - Vecenergy - 4251",
                "parentName": "terminal_city",
                "parentValue": "Albuquerque",
                "children": [],
                "canDisplay": false,
                "showRecord": true,
                "$$hashKey": "object:31718"
              }
            ],
            "canDisplay": false,
            "showRecord": true,
            "$$hashKey": "object:31711"
          }
        ],
        "canDisplay": false,
        "showRecord": true,
        "$$hashKey": "object:31518"
      },
      {
        "name": "terminal_state",
        "value": "NV",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31519"
      },
      {
        "name": "terminal_state",
        "value": "NY",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31520"
      },
      {
        "name": "terminal_state",
        "value": "OH",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31521"
      },
      {
        "name": "terminal_state",
        "value": "OK",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31522"
      },
      {
        "name": "terminal_state",
        "value": "OR",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31523"
      },
      {
        "name": "terminal_state",
        "value": "PA",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31524"
      },
      {
        "name": "terminal_state",
        "value": "RI",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31525"
      },
      {
        "name": "terminal_state",
        "value": "SC",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31526"
      },
      {
        "name": "terminal_state",
        "value": "SD",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31527"
      },
      {
        "name": "terminal_state",
        "value": "TN",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31528"
      },
      {
        "name": "terminal_state",
        "value": "TX",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31529"
      },
      {
        "name": "terminal_state",
        "value": "UT",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31530"
      },
      {
        "name": "terminal_state",
        "value": "VA",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31531"
      },
      {
        "name": "terminal_state",
        "value": "WA",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31532"
      },
      {
        "name": "terminal_state",
        "value": "WI",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31533"
      },
      {
        "name": "terminal_state",
        "value": "WY",
        "parentName": "padd",
        "parentValue": "Unknown",
        "children": [],
        "canDisplay": true,
        "showRecord": true,
        "$$hashKey": "object:31534"
      }
    ],
    "canDisplay": true,
    "showRecord": true
  }
]




问题是我无法遍历jsons并推送json1.i中不匹配的键想要比较json1和json2而json1中缺少的那些键应该用键更新当我从json2复制到json1时,我想更新密钥candisplay。请帮助我是javascript的新手,我对编程没有任何了解

1 个答案:

答案 0 :(得分:0)

var L = json1.length;
var L1 = json2.length;
 for (var i = 0; i < L; i++) {
  var obj = json1[i];
   for (var j in obj) {
    for (var x = 0; x < L1; x++) {
        var obj1 = json2[x];
        for (var k in obj1) {
            if (i == x && j == k) {
                if (obj[j] == obj1[k]) {
                    continue;
                } 
                else 
                {
                    json1[i][j] = json2[x][k];
                }
            }
        }
    }
  }
}

for(i in json1){
 var obj2=json1[i]
 for( j in obj2)
 if(json1[i][j]===false)
json1[i][j]=true

}

是我试图迭代上面给出的json并且还添加缺失值的代码