Why are items in the array being skipped?

foreach javascript
javascript array
javascript map
array splice
typescript foreach
javascript loop through array and remove items
javascript array methods
javascript array push

I have a table

I have the following script

$Result = Query "SELECT [databasename], [servertypeA], [servertypeB] from table GROUP BY [databasename], [servertypeA], [servertypeB]"

$Servers = @()
$DB = @()

foreach($row in $Result)
{   
    $i++
    $DB += $row.Item("databasename")
    $Servers += $row.Item("servertypeA")
    $Servers += $row.Item("servertypeB")

    cmd /c "PS.bat $somescript.ps1 $($Servers[-1]) $($DB[-1])"
}

this results in:

db1 serverx
db1 servery
db2 serverx
db2 servery

it should be

db1 server1
db1 serverx
db1 servery
db2 server2
db2 serverx
db2 servery

why is servertypeA being skipped, i.e. server1 and server2 in $Server?

I also tried this

$Result = Query "SELECT distinct [databasename], [servertypeA], [servertypeB] from table"

and its generating the same result

The big change is to the query, to get the results you want, but note this also let me greatly simplify the loop:

$Result = Query "SELECT DISTINCT [databasename], [serverTypeA] As Server from table  UNION SELECT DISTINCT [databasename], [serverTypeB] from table"

foreach($row in $Result)
{   
    $DB = $row.Item("databasename")
    $Server = $row.Item("Server")

    cmd /c "PS.bat $somescript.ps1 $Server $DB"
}

Some values are being skipped when copying value from an array , It's because you remove items in the array while looping . I mean the following line codearray.shift();. You can workaround this by the solution as stated below The reason for this is that the iterator does not know that a list element was removed, and happily advances to the next item. In the above example, on the first iteration, the iterator looks at the first element, the 1. In the loop body, the 1 is removed from the list, making 2 the first element in the list.

replace this below code

$DB += $row.Item("databasename")
$Servers += $row.Item("servertypeA")
$Servers += $row.Item("servertypeB")

with

$DB += $row.Item("databasename")
$Servers += $row.Item("servertypeA")
$DB += $row.Item("databasename")
$Servers += $row.Item("servertypeB")

Common pitfalls when working with Javascript Arrays, Apart from being much slower than regular arrays, they can be quite problematic plus one, and that is not necessarily the number of objects in the array! If you think I missed anything, please let me know in the comments. Why are items in the array being skipped? Ask Question Asked 9 months ago. Active 9 months ago. Viewed 44 times 0. I have a table. I have the following script

Remove items from Array while iterating over it. (Example), A protip by namuol about algorithms, array, and javascript. results when items are removed from an array while the array is being iterated over? then the for loop triggers ++i , which skips to the next next item, leaving The Take and Skip methods are functional complements. Given a sequence coll and an integer n , concatenating the results of coll.Take(n) and coll.Skip(n) yields the same sequence as coll . In Visual Basic query expression syntax, a Skip clause translates to an invocation of Skip .

How to Use forEach() to Iterate an Array in JavaScript, forEach() in JavaScript calls the provided function on each array item with 3 arguments: item, index, This is where forEach() array method can be helpful. forEach() skips the empty slots of the array (named sparse array). So I have a data table with 19 columns and 19 corresponding items in my aoColumns array. I kept getting this error

Removing items from a list while iterating over the list, When removing items from a list while iterating over the same list, a naive That way, no elements can be skipped since removing an item from the list will only� count items in a column vaadin. java,vaadin. Columns don't contain items, Rows contain items. You can set the visible columns by passing a array to the setVisibleColumns methos of the Table. It could also be a idea, to just colapse the column, not hiding it Determining if all values of this colum are empty should be simple

Array.prototype.forEach(), If existing elements of the array are changed or deleted, their value as (e.g. using shift() ) during the iteration, later elements will be skipped. This Items property captures all items purchased in the order and it is always synced and stored as an array, which is the list data type in Klaviyo. This allows us to store the one property -- Items -- but have that property include a series of values (e.g., each item purchased in the single order), where each value in the array can be

Comments
  • The query you wrote only returns four rows.
  • so then i dont need $Servers = @() $DB = @()?
  • You only need them if you want to use them later on. You don't need them to call this script. And even if you do want to use them later, I would say it's better to only keep one collection, where the DB and the Server properties of the object in the collection, to better preserve the relationship between db and server.
  • so i just changed the code to how you have it, and this resulted in: db1 server1 first iteration, second iteration: db1db1 server1serverx, 3rd iteration db1db1db1 server1serverxservery. its now apending to the previous iterations
  • Make sure $Server doesn't still show a += and isn't previously declared as an array.
  • THAT was it! THANK YOU :D. also i dont need the arrays nor do i need the -1 complex code. your script is much more efficient!
  • just tried this. it still results in same output im getting, skipping server1 and server2
  • In the other script i have defined it this way: param( [string] $Server, [string] $DB ). unfortunately, i cant make $Server an array in this other script because its being passed directly as a connection string parameter.
  • also i tried this and it results in server1 serverx, server1 servery, server 2 serverx, server2 servery, basically skipping the $DB entirely
  • Yeah, that'll be because it's positionally binding to the param block in your other script, and only the first two items are going into the params. If you were to look, you'd find $DB in the $args automatic variable in the script. But anyway, I've added an edit to my answer that calls yr script twice per loop. HTH