javascript:嵌套for循环

时间:2016-08-18 20:11:51

标签: javascript angularjs underscore.js

在我的角度应用中,我有两个数组,如下所示:

$scope.data = ["val 10,val 11,val 12", "val 20,val 21,val 22", "val 30,val 31,val 32"];

$scope.fields = [ "key1", "key2", "key3" ];

现在我需要使用以下格式将数组($scope.data)的每个数据发送到服务器:

{
  key1: 'val 10',
  key2: 'val 11',
  key2: 'val 12',
}
{
  key1: 'val 20',
  key2: 'val 21',
  key2: 'val 22',
}
{
  key1: 'val 30',
  key2: 'val 31',
  key2: 'val 32',
}  

但问题是:当我向服务器发送数据时,我只获得$scope.data的最后一个值。我的代码如下:

// $scope.data = ["val 10,val 11,val 12", "val 20,val 21,val 22", "val 30,val 31,val 32"];
_.each($scope.data, function(result, index) {

  var dataArray = result.split(",");  // ["val 10" ,"val 11", "val 12"]
                                      // ["val 20" ,"val 21", "val 22"]
                                      // ["val 30" ,"val 31", "val 32"]

  _.each(dataArray, function(info, position) {

    if( info !== null || info !== '' || info.length > 0 )  {
      $scope.data[ $scope.fields[position] ] = info;   // { key1: 'val 10', key2: 'val 11', key2: 'val 12' }                                                        
                                                      //  { key1: 'val 20', key2: 'val 21', key2: 'val 22' }
                                                      //  { key1: 'val 30', key2: 'val 31', key2: 'val 32' }
    }
  });

  $http
    .post(url + '/import-data', {
      "data": $scope.data
    })
    .then(function (res) {
      console.log(res);  // in server I always get the following object
                         //  { key1: 'val 30', key2: 'val 31', key2: 'val 32' }
                         //  { key1: 'val 30', key2: 'val 31', key2: 'val 32' }
                         //  { key1: 'val 30', key2: 'val 31', key2: 'val 32' }
    }, function (error) {
      console.log("error : ", error);
    })

});

我该如何解决这个问题?在此先感谢...

2 个答案:

答案 0 :(得分:1)

我为你创建了一个名为$scope.serverdata的新变量。将其传递给服务器。

$scope.data = ["val 10,val 11,val 12", "val 20,val 21,val 22", "val 30,val 31,val 32"];
 $scope.fields = ["key1", "key2", "key3"];
 $scope.serverdata = [];
 angular.forEach($scope.data, function(result) {
     var dataArray = result.split(",");

     var value = {};
     angular.forEach($scope.fields, function(field, index) {
         if (value[field] == undefined) {
             value[field] = dataArray[index];
         };

     });
     $scope.serverdata.push(value);
 });
 });

$scope.serverdata现在看起来像

{
  key1: 'val 10',
  key2: 'val 11',
  key3: 'val 12',
}
{
  key1: 'val 20',
  key2: 'val 21',
  key3: 'val 22',
}
{
  key1: 'val 30',
  key2: 'val 31',
  key3: 'val 32',
}  

这是小提琴链接

http://fiddle.jshell.net/LXAt7/655/

使用

拨打电话
$http
    .post(url + '/import-data', {
      "data": $scope.serverdata
    })
    .then(function (res) {
      console.log(res); 
    }, function (error) {
      console.log("error : ", error);
    })

答案 1 :(得分:1)

试试这个:

_.each($scope.data, function(result, index) {

    var dataArray = result.split(","); 

    $http
       .post(url + '/import-data', {
            "data": _.object( $scope.fields, dataArray)
       })
      .then(function (res) {
          console.log(res); 
       }, function (error) {
          console.log("error : ", error);
       })

  });