如何将参数返回给数组

时间:2018-06-18 15:00:36

标签: javascript angularjs

我有以下代码:

  function increment(index, type) {
    $scope.items[index].type++;
    console.log(type);
    console.log($scope.items[index].type);
  };

当我console.log(type)时,我得到正确的字符串。

但是,console.log($scope.items[index].type)会返回NaN

知道如何将参数添加到数组列表中吗?

2 个答案:

答案 0 :(得分:0)

如果type是一个字符串,那么

$scope.items[index].type++;

console.log($scope.items[index].type);

会做你期待的事情。

您需要使用type作为数组/对象索引。

function increment(index, type) {
    $scope.items[index][type]++;
    console.log(type);
    console.log($scope.items[index][type]);
};

答案 1 :(得分:0)

您没有提供所需的信息。请提供项目数组和内容的结构。

但是,如果我读了你的代码,我可以假设该类型在某种程度上是数组中每个项的属性。所以这很好....

let items = [{type: 5}, {type: 6}, {type: 7}];

increment(2, "no clue what this argument is about");

function increment(index, type) {
    items[index].type++;
    document.write(type);
    document.write("<br/>" + items[index].type);
 };

相关问题