Do I have to use $.each()?
You'll need some kind of looping construct, yes. A for
loop, for instance, or the features of Array.prototype
, or in ES2015 (aka "ES6") and later a for-of
loop, or yes, you could use jQuery's $.each
or $.map
. For instance, we can get an array of those values using arrStages.map
:
var days = arrStages.map(function(entry) {
return entry.strNumOfDays;
});
Or in ES2015+:
let days = arrStages.map(entry => entry.strNumOfDays);
Example (in ES5):
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 },
];
var days = arrStages.map(function(entry) {
return entry.strNumOfDays;
});
console.log(days);
This question's answers cover the wide number of options you have for looping through arrays in JavaScript.
In ES2015 and above, if you don't need an array but you just need to loop through the values, here's what a for-of
loop would look like:
for (let entry of arrStages) {
// Use entry.strNumOfDays here...
}