如果对象中的键相同,则更改值

时间:2019-07-16 13:25:29

标签: javascript jquery html json dictionary

我有一个数组JSON列表。我想替换一个特定的键值。例如,如果状态为active,将active更改为Publish,或者如果状态为inactive,则将inactive替换为Delist

$.getJSON("http://192.168.1.211:3000/facility/", function(results) {
  var buildingtitle1 = '';
  var building = results.length;

  var items = [{
    "status": "active"
  }, {
    "status": "inactive"
  }];

  var newItem = {
    "status": "active",
    "color": "Publish"
  }

  var inactiveItem = {
    "status": "inactive",
    "color": "Delist"
  }

  items.forEach(function(item) {
    if (newItem.status === item.status) {
      item.color = newItem.color
    } else if (inactiveItem.status === item.status) {
      item.color = inactiveItem.color
    }
  });

  for (i = 0; i <= building - 1; i++) {
    buildingtitle1 += '<tr class="delete_building">' +
      '<td>' + results[i].facilityid + '</td>' +
      '<td><button class="btn btn-light buildingname _id" type="button" value="' + results[i]._id + '">' + results[i].name + '</button></td>' +
      '<td><button class="btn btn-light buildingname City" type="button" value="' + results[i].address.city + '">' + results[i].address.city + '</button></td>' +
      '<td><button class="btn btn-light buildingname Locality" type="button" value="' + results[i].address.locality + '">' + results[i].address.locality + '</button></td>' +
      '<td><button class="btn btn-light buildingname State" value="" type="button">' + results[i].status + '</button></td>' +
      '<td><button class="btn btn-light buildingname Buildingtype" value="' + results[i].buildingtype + '" type="button">' + results[i].buildingtype.replace(/([A-Z])/g, ' $1').trim() + '</button></td>' +
      '<td><button class="btn btn-primary btn-sm btn-danger Delete_bul" value="' + results[i]._id + '" type="button">Action</button></td>' +
      '</tr>';
  };

  $("#buildingtitle > tbody").html(buildingtitle1);
});

0 个答案:

没有答案