迭代数组项并检查属性值

时间:2016-03-08 12:24:29

标签: javascript jquery arrays

    function GetViewModelData() {
    var RDcViewModel = [];
    var recordId = $.trim($("#recordId").val());

    for (i = 1; i <= rowCount; i++) {
        var item1 = $.trim($("#item1" + i).val()) == '' ? 0 : parseInt($("#item1" + i).val());
        var item2 = $.trim($("#item2" + i).val()) == '' ? 0 : parseInt($("#item2" + i).val());
        var GrandTotal = (item1 + item2);
        var rdtCViewModel = new ItemDetailsViewModel(0, item1, item2, GrandTotal);
        RDcViewModel.push(rdtCViewModel);
    }

    var obj = new ReportViewModel(recordId, RDcViewModel);
    var viewmodel = JSON.stringify(obj);
    return viewmodel;
}

我有上面的示例函数,我用它迭代html表行并将行值存储在数组中。

一旦我填充了我的阵列,我就会使用下面的代码片段将数据发布到我的控制器。

var PostData = function () {
$(".btnSubmit").click(function () {
    var viewmodel = GetViewModelData();
    //i want to check from here if viewmodel has any item(row) where GrandTotal is 0 (zero)
    $.ajax({
        async: true,
        cache: false,
        contentType: 'application/json; charset=utf-8',
        data: viewmodel,
        headers: GetRequestVerificationToken(),
        type: 'POST',
        url: '/' + virtualDirectory + '/Item/DataSave',
        success: function (data) {
            if (data == true) {
                window.location.href = '/' + virtualDirectory + '/Destination/Index';
            }
        },
        error: function (e) {
            return false;
        }
    });
});

}

我现在想要在PostData函数中做的是检查我的“viewmodel”对象是否包含“GrandTotal”为0的任何项目(行)。

3 个答案:

答案 0 :(得分:1)

正如您已将其字符串化,现在如果要访问其键和值,则必须将其解析回来:

var PostData = function() {
  $(".btnSubmit").click(function() {
    var viewmodel = GetViewModelData(),
        viewObj = JSON.parse(viewmodel),
        flag = false; // <-----parse it back here

    viewObj.forEach(function(i, el){
        flag = el.GrandTotal === 0;
        return flag;
    });

    if(flag){ return false; }  // <------ and stop it here.


    $.ajax({
      async: true,
      cache: false,
      contentType: 'application/json; charset=utf-8',
      data: viewmodel,
      headers: GetRequestVerificationToken(),
      type: 'POST',
      url: '/' + virtualDirectory + '/Item/DataSave',
      success: function(data) {
        if (data == true) {
          window.location.href = '/' + virtualDirectory + '/Destination/Index';
        }
      },
      error: function(e) {
        return false;
      }
    });
  });
}

答案 1 :(得分:1)

使用JSON.parse(viewmodel),使用类型为ReportViewModel的{​​{1}} JS数组准备RDcViewModel类型的对象并对其进行迭代,以查找{{1}是否有任何grandtotal == 0实例

ItemDetailsViewModel

答案 2 :(得分:0)

再次迭代数组没有意义。打破GetViewModelData()中的循环并从该函数返回false。然后在PostData中测试它

现有的for循环:

var GrandTotal = (item1 + item2);
if(!GrandTotal){
   return false; 
}

然后在PostData()

var PostData = function () {
$(".btnSubmit").click(function () {
    var viewmodel = GetViewModelData();
    if(viewmodel === false){
      alert('Missing total');
      return; //don't proceed
     }
     /* your ajax */