#Inside the function the arrayList has 51 items, but the function returns a 102 items arrayList #0 to #51 are int values 0 to 51, #51 to #101 are the expected json objects?
function Get-RedditTopics () {
$arrayList = New-Object -TypeName "System.Collections.ArrayList"
$url = 'https://www.reddit.com/r/worldnews/.json'
$pages = 2
$after = ""
for ($i = 0; $i -lt $pages; $i++) {
if ($i -gt 0) {
$url = $url + "?after=$after"
}
$result = Invoke-RestMethod $url
foreach ($item in $result.data.children) {
$arrayList.Add($item.data)
}
$after = $result.data.after
}
return $arrayList #51 items, ok!
}
$redditTopics = Get-RedditTopics #function returns $arrayList with 102 items
write-host "redditTopics count: $($redditTopics.Count)"