0

I have a JSON like this:

var arrStages = [                 
    { "strID" : "ID-0001" , "intStageNum" : 1 , "strNumOfDays": 1 }, 
    { "strID" : "ID-0003" , "intStageNum" : 3 , "strNumOfDays": 14}, 
    { "strID" : "ID-0002" , "intStageNum" : 2 , "strNumOfDays": 3 },
    { "strID" : "ID-0006" , "intStageNum" : 6 , "strNumOfDays": 3 },
    { "strID" : "ID-0004" , "intStageNum" : 4 , "strNumOfDays": 3 },
    { "strID" : "ID-0005" , "intStageNum" : 5 , "strNumOfDays": 3 },
];

How can I get the value of a specific attribute of each record (intStageNum for example) but I have to do it backwards. I have to start the loop from the last record which is

{ "strID" : "ID-0005" , "intStageNum" : 5 , "strNumOfDays": 3 }

Is there any way to do this?

Pooja Kedar
  • 439
  • 2
  • 10
  • 23
AyakoS
  • 221
  • 2
  • 7
  • 18

6 Answers6

2

Try with below loop:

var arrStages = [                 
    { "strID" : "ID-0001" , "intStageNum" : 1 , "strNumOfDays": 1 }, 
    { "strID" : "ID-0003" , "intStageNum" : 3 , "strNumOfDays": 14}, 
    { "strID" : "ID-0002" , "intStageNum" : 2 , "strNumOfDays": 3 },
    { "strID" : "ID-0006" , "intStageNum" : 6 , "strNumOfDays": 3 },
    { "strID" : "ID-0004" , "intStageNum" : 4 , "strNumOfDays": 3 },
    { "strID" : "ID-0005" , "intStageNum" : 5 , "strNumOfDays": 3 },
];

for (var i=(arrStages.length-1); i>=0; i--){
  console.log(arrStages[i]['intStageNum']);
}
Ngoan Tran
  • 1,507
  • 1
  • 13
  • 17
1
for (var i=arrStages.length - 1; i >= 0; i--){
    console.log(arrStages[i].intStageNum);
}
Ju66ernaut
  • 2,592
  • 3
  • 23
  • 36
0

The other answers have been giving you an iterative approach: I think it is better to use the forEach or map approach, as it makes the code easier to read.

Some solutions are simply writing arrStages["intStageNum"] within the for loop. This is not extensible if you want to find some other attribute.

Instead, we define a function findElement that takes two arguments array and element. These arguments should be self explanatory.

Lastly, we do not want to mutate the array so we should not call array.reverse. Instead, this SO post tells us to use array.slice.reverse instead.

    var arrStages = [                 
        { "strID" : "ID-0001" , "intStageNum" : 1 , "strNumOfDays": 1 }, 
        { "strID" : "ID-0003" , "intStageNum" : 3 , "strNumOfDays": 14}, 
        { "strID" : "ID-0002" , "intStageNum" : 2 , "strNumOfDays": 3 },
        { "strID" : "ID-0006" , "intStageNum" : 6 , "strNumOfDays": 3 },
        { "strID" : "ID-0004" , "intStageNum" : 4 , "strNumOfDays": 3 },
        { "strID" : "ID-0005" , "intStageNum" : 5 , "strNumOfDays": 3 }
    ];
    
    
    function findElement(array, attr) {
     array.forEach((element) => {
        console.log(element[attr]);
      });
    }
    
    findElement(arrStages.slice().reverse(), "intStageNum");
Community
  • 1
  • 1
Lieu Zheng Hong
  • 676
  • 1
  • 10
  • 22
0

How about using "reduceRight"?

arrStages.reduceRight(function(nothing, record){
     yourCheckFunction(record);
},0);
blackmiaool
  • 5,234
  • 2
  • 22
  • 39
0

2 ways.

1) the usual way. use i-- loop coming back from arrStages.length-1 to arrStage[0]

2) do while loop and pop element as you go. (Assuming you need to free up the memory by deleting elements from the end of the array.

Since both are going through n object, so it's O(n). You really don't get any benefit on performance here. But it's just an alternative.

I personally don't like .reverse much because after you reverse it, you still need to go through the for loop again. It's looping twice.. but :p I don't think that matters.

var arrStages = [                 
    { "strID" : "ID-0001" , "intStageNum" : 1 , "strNumOfDays": 1 }, 
    { "strID" : "ID-0003" , "intStageNum" : 3 , "strNumOfDays": 14}, 
    { "strID" : "ID-0002" , "intStageNum" : 2 , "strNumOfDays": 3 },
    { "strID" : "ID-0006" , "intStageNum" : 6 , "strNumOfDays": 3 },
    { "strID" : "ID-0004" , "intStageNum" : 4 , "strNumOfDays": 3 },
    { "strID" : "ID-0005" , "intStageNum" : 5 , "strNumOfDays": 3 },
];

while (arrStages.length > 0) {
  poped = arrStages.pop();
  console.log(poped.intStageNum);
}
0

You can first reverse the array with reverse() method and then start iterating it with foreach loop. as reverse() is mutating array so you can you try something like following

var arrStages = [                 
    { "strID" : "ID-0001" , "intStageNum" : 1 , "strNumOfDays": 1 }, 
    { "strID" : "ID-0003" , "intStageNum" : 3 , "strNumOfDays": 14}, 
    { "strID" : "ID-0002" , "intStageNum" : 2 , "strNumOfDays": 3 },
    { "strID" : "ID-0006" , "intStageNum" : 6 , "strNumOfDays": 3 },
    { "strID" : "ID-0004" , "intStageNum" : 4 , "strNumOfDays": 3 },
    { "strID" : "ID-0005" , "intStageNum" : 5 , "strNumOfDays": 3 },
];
arrStages.reverse();
arrStages.forEach((element) => {
    console.log(element);
});

or you can start your loop with last index to first like this

for(var i=arrStages.length - 1; i >= 0; i--)

and as you need to iterate all the array so for O(n) complexity if you are having much larger arrays then use for loop from end to start.

Ridham Tarpara
  • 5,970
  • 4
  • 19
  • 39