Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将两个js列表映射到一个成对列表?_Javascript_Arrays_Angularjs - Fatal编程技术网

Javascript 如何将两个js列表映射到一个成对列表?

Javascript 如何将两个js列表映射到一个成对列表?,javascript,arrays,angularjs,Javascript,Arrays,Angularjs,我有两个相同长度的列表 我想使用angular js创建一个重复的ui表 <tr ng-repeat="pair in pairs ..> 我该怎么做 将两个列表连接起来,然后以角度重复?或者进行双重重复?您可以使用迭代器中的“$index”从另一个数组中获取值 //controller $scope.pairs = [1, 2, 3]; $scope.otherArray = ['a', 'b', 'c']; //template <tr ng-repeat="pair

我有两个相同长度的列表

我想使用angular js创建一个重复的ui表

 <tr ng-repeat="pair in pairs ..>
我该怎么做


将两个列表连接起来,然后以角度重复?或者进行双重重复?

您可以使用迭代器中的“$index”从另一个数组中获取值

//controller
$scope.pairs = [1, 2, 3];
$scope.otherArray = ['a', 'b', 'c'];
//template
<tr ng-repeat="pair in pairs">
   <td>{{ pair }}</td>
   <td>{{ otherArray[$index] }}</td>
</tr>
//控制器
$scope.pairs=[1,2,3];
$scope.otherArray=['a','b','c'];
//模板
{{pair}}
{{otherArray[$index]}

这将适用于长度相同的数组(这是您必须假设的)。

您可以使用迭代器中的“$index”从另一个数组中获取值

//controller
$scope.pairs = [1, 2, 3];
$scope.otherArray = ['a', 'b', 'c'];
//template
<tr ng-repeat="pair in pairs">
   <td>{{ pair }}</td>
   <td>{{ otherArray[$index] }}</td>
</tr>
//控制器
$scope.pairs=[1,2,3];
$scope.otherArray=['a','b','c'];
//模板
{{pair}}
{{otherArray[$index]}
这将使数组的长度相同(这是您必须假设的)

  • 使用
    list1
    上的
    ng repeat
  • 使用表达式(
    {{}
    )打印其元素
  • 使用
    $index
    作为索引从另一个数组中获取值
  • 你就是这样做的

    angular.module('app',[]).controller('ctrl',function($scope){
    $scope.list1=[1,2,3,4];
    $scope.list2=[“a”、“b”、“c”、“d”];
    })
    
    {{list}}行:{{list}{{{list2[$index]}
    
  • 使用
    list1
    上的
    ng repeat
  • 使用表达式(
    {{}
    )打印其元素
  • 使用
    $index
    作为索引从另一个数组中获取值
  • 你就是这样做的

    angular.module('app',[]).controller('ctrl',function($scope){
    $scope.list1=[1,2,3,4];
    $scope.list2=[“a”、“b”、“c”、“d”];
    })
    
    {{list}}行:{{list}{{{list2[$index]}
    
    最好的方法是将两个阵列合并为一个,例如:

    var $scope.pairs = [] ;
    
    for (var i = 0 ; i < Array_1.length ; i++) {
        $scope.pairs.push( {number: Array_1[i] ,
                            letter: Array_2[i]  } ;
    }
    
    var$scope.pairs=[];
    对于(var i=0;i
    以及您的html:

    <tr ng-repeat="pair in pairs>
       <td>
          {{pair.number}} - {{pair.letter}}
       </tr>
    </tr>
    

    最好的方法是将两个数组合并为一个,例如:

    var $scope.pairs = [] ;
    
    for (var i = 0 ; i < Array_1.length ; i++) {
        $scope.pairs.push( {number: Array_1[i] ,
                            letter: Array_2[i]  } ;
    }
    
    var$scope.pairs=[];
    对于(var i=0;i
    以及您的html:

    <tr ng-repeat="pair in pairs>
       <td>
          {{pair.number}} - {{pair.letter}}
       </tr>
    </tr>
    

    请更详细地阐述你的问题。考虑到这两个数组,你想要的输出是什么?对不起,忘记了。添加了这些列表是否始终具有相同的长度@EladBenda?是的。我这样设置请更详细地阐述你的问题。考虑到这两个数组,你想要的输出是什么?对不起,忘记了。添加了这些列表是否始终具有相同的长度@E拉本达?是的,我就是这么定的