将数组项调用到另一个数组中

时间:2016-01-29 23:05:57

标签: javascript angularjs

我面对以下功能(或者我不知道正确名称的方法):

function getRowArray($scope, object, i){    
  i = i + 1;
  var item = {};
  var data = [];
  var id = -1;    

  if ($scope.selectedType !== undefined) {
    id = $scope.selectedType.id;
  }    

  var rating = getRating($scope, object, id);    
  item['name'] = $scope.objectInfo[object]['name'];
  item['objectId'] = rating.objectId;
  item['hideRating'] = parseInt($scope.objectInfo[object].hideControls) & 1;   
  item['addInfo'] = rating.addInfo;   
  item['rating'] = rating.value;    
  item['ratingId'] = rating.id;

  for (var i in $scope.objectInfo[object].childs) {        
    if ($scope.objectInfo[object].childs[i] == object){
        continue;
    }

    data.push(getRowArray($scope, $scope.objectInfo[object].childs[i], i));
  }    
  item['data'] = data;
  return item;
}

function getTypeRow($scope, oobject, otype){

  var item = {};
  var data = [];
  var rating = getRating($scope, oobject.id, otype.id);

  item['name'] = otype.name;
  item['objectId'] = rating.objectId;
  item['typeId'] = rating.typeId;
  item['ratingId'] = rating.id;
  item['addInfo'] = rating.addInfo;
  item['rating'] = rating.value;
  // item['hideRating'] = parseInt($scope.objectInfo[object].hideControls);

  return item;
}

我想在第二个中使用第一个中的hideRating项目,我尝试并添加了注释行,但我得到一个错误,它说对象是unifined,是错误的,还是我错过了什么?提前谢谢

1 个答案:

答案 0 :(得分:1)

object未定义,因为它尚未初始化;它未在函数getTypeRow的参数列表中指定。参数列表中的oobject应更正为object

// Correct 'oobject' to 'object'
function getTypeRow($scope, object, otype){

  var item = {};
  var data = [];
  // Correct 'oobject' to 'object'
  var rating = getRating($scope, oobject.id, otype.id);

  ...
}