Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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在对象数组中循环_Javascript_Arrays - Fatal编程技术网

Javascript在对象数组中循环

Javascript在对象数组中循环,javascript,arrays,Javascript,Arrays,我想使用JavaScript在对象数组中循环 console.log($scope.filteredItems); 返回控制台中包含10个对象的数组 Object {editionId: "6", detailId: "10"} [Object, Object, Object, Object, Object, Object, Object, Object, Object, Object] 0: Object 1: Object 2: Object 3: Object 4: Object 5:

我想使用JavaScript在对象数组中循环

console.log($scope.filteredItems);
返回控制台中包含10个对象的数组

Object {editionId: "6", detailId: "10"} 
[Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]
0: Object
1: Object
2: Object
3: Object
4: Object
5: Object
6: Object
7: Object
8: Object
9: Object
length: 10
__proto__: Array[0]
它的当前选择是10

我想使用“上一步”和“下一步”按钮循环浏览这个已过滤的数组

html


请注意,这是一个从数据库中筛选出的数组,递增可能只是循环执行 整个数据库不是筛选的数组

这是我的努力

if ($scope.filteredItems.detailId > 0) {

    console.log($route.params);
    $scope.prevItem = Number($scope.filteredItems.detailId)-1;
} else {
    $scope.prevItem = $scope.data.products.length-1;
}

if ($scope.filteredItems.detailId < $scope.data.products.length-1) {
    $scope.nextItem = Number($scope.filteredItems.detailId)+1;
} else {
    $scope.nextItem = 0;
}
if($scope.filteredItems.detailId>0){
console.log($route.params);
$scope.previitem=Number($scope.filteredItems.detailId)-1;
}否则{
$scope.previItem=$scope.data.products.length-1;
}
if($scope.filteredItems.detailId<$scope.data.products.length-1){
$scope.nextItem=Number($scope.filteredItems.detailId)+1;
}否则{
$scope.nextItem=0;
}
试试这个:

HTML:

<button id="prev">Prev</button>
<button id="next">Next</button>

<div id="data_value"></div>
Prev
下一个
JS代码:

$(document).ready(function(){
    var counter = 0;
    var arrObj = [{name:"sam1",age:20},{name:"sam2",age:22},{name:"sam3",age:24},{name:"sam4",age:26},{name:"sam5",age:28}];
    var totalcounter = 0;

    for (i in arrObj) {
        totalcounter++;
    }

    $("#data_value").text(arrObj[0].name+" - "+ arrObj[0].age);
    $("#prev").attr('data-rel',counter);
    $("#next").attr('data-rel',counter);

    $("#next").click(function(){
        if (counter < totalcounter - 1) {
            ++counter;
            $("#data_value").text(arrObj[counter].name+" - "+ arrObj[counter].age);
        }
    });

    $("#prev").click(function(){
        if (counter > 0) {
            --counter;
            $("#data_value").text(arrObj[counter].name+" - "+ arrObj[counter].age);
        }
    });       
}); 
$(文档).ready(函数(){
var计数器=0;
var arrObj=[{姓名:“sam1”,年龄:20},{姓名:“sam2”,年龄:22},{姓名:“sam3”,年龄:24},{姓名:“sam4”,年龄:26},{姓名:“sam5”,年龄:28}];
var totalcounter=0;
(我在阿罗布j){
totalcounter++;
}
$(“#数据_值”).text(arrObj[0].name+“-”+arrObj[0].age);
$(“#prev”).attr('data-rel',计数器);
$(“#下一步”).attr('data-rel',计数器);
$(“#下一步”)。单击(函数(){
如果(计数器<总计数器-1){
++计数器;
$(“#数据_值”).text(arrObj[counter].name+“-”+arrObj[counter].age);
}
});
$(“#prev”)。单击(函数(){
如果(计数器>0){
--计数器;
$(“#数据_值”).text(arrObj[counter].name+“-”+arrObj[counter].age);
}
});       
}); 

为了在DIR=+1 |-1方向上通过长度LEN数组,从上一个位置循环到下一个位置,请执行以下操作:

NEXT = (PREV + DIR + LEN) % LEN

为什么在JavaScript中用
$
作为变量的前缀?这更像是一个PHP的东西。在JQuery中,您通常使用
$
作为JQuery对象的前缀,因此对常规变量使用该符号会混淆它的角度js$scope,但它更像是一个常规查询,将$scope.filtereditems替换为bananaHi感谢您刚刚在JSFIDLE中尝试了这一点,并且数据值没有被返回Hansk Sandeep您的a星,如果我使用var arrObj=$scope.filteredItems,这会起作用吗?
NEXT = (PREV + DIR + LEN) % LEN