-1

I have a list of /24 IP addresses in an array called subRet

subRet's values are like 10.0.0.1 10.0.0.2 10.0.0.50 10.0.0.80

What I want is a list of IP address that are NOT in the array.

What I've tried is this:

 var test=['10.0.0.1','10.0.0.103','10.0.0.111','10.0.0.131','10.0.0.198'];
 for(i=1;i<=254;i++){
            if( ! $.inArray('10.0.0.'+i.toString(), test ) ) {
                console.log("adding "+'10.0.0.'+i.toString());
            }
  }     

Console log says

adding 10.0.0.1 

What I want is a list if IP's that are not in the list, like 10.0.0.2.

how?

Shawn
  • 3,031
  • 4
  • 26
  • 53
  • 3
    First I'd put the array's values into a set/object/etc so you don't have to iterate the array each time. Then just check if it's in the set/object/etc. I see zero value in using jQuery for this. – Dave Newton Jan 16 '20 at 21:39
  • I used used test.includes instead and that seems to work, thanks – Shawn Jan 16 '20 at 21:42
  • 1
    Just keep in mind that `includes` will iterate over the `test` array for every value you pass in. It likely doesn't *matter*, but IMO using a set/etc. is cleaner, and communicates the intent better. – Dave Newton Jan 16 '20 at 21:44

3 Answers3

2

First, you're not declaring the variable i anywhere, which is bad practice as the scope will likely not be what you expect. Secondly, no need for jQuery here, you can use vanilla ES6. Third, no need to call i.toString as you're concatenating it to a string already, which performs implicit casting.

var test=['10.0.0.1','10.0.0.103','10.0.0.111','10.0.0.131','10.0.0.198'];
for(let i = 1; i <= 254; i++){
   if(!test.includes('10.0.0.' + i)) {
       console.log("adding 10.0.0." + i);
   }
}     
IceMetalPunk
  • 5,476
  • 3
  • 19
  • 26
  • I'm declaring i... I just didn't it in my sample. – Shawn Jan 16 '20 at 21:40
  • If you're declaring it outside the for loop, the value you get in the console log might not be the actual value for the iteration you're on, since it'll be in a different scope and console logs can be asynchronous. Declaring it in the loop with `let` forces it to be block scoped to the loop body, which gives the result you're expecting. – IceMetalPunk Jan 16 '20 at 21:41
  • @IceMetalPunk console logs cannot be asynchronous (unless called from a worker). it's not necessary to use "let" here (although i do agree that it's good practice, if all target browsers support it) – obe Jan 16 '20 at 21:45
  • @obe It can be synchronous, asynchronous, or synchronous-but-delayed, depending on implementation: https://stackoverflow.com/a/23392650/2444210 – IceMetalPunk Jan 16 '20 at 21:47
  • @IceMetalPunk ok, i'll rephrase. console.log() is not asynchronous for all intents and purposes. can you find one example for a browser or debug tool (that people actually use) that implements console.log() in such a way that it would not be reliable in the above example? (and, also, how would using `let` make it any less asynchronous..?) – obe Jan 16 '20 at 21:53
0

The issue of your code is in the way you check the result of the method inArray, since inArray method retrieves the index of the tested IP in the array, and when it finds nothing it returns -1. So you need to make sure that the returned index is higher than -1:

 if( ! $.inArray('10.0.0.'+i.toString(), test ) > -1)

Read more about inArray

Abdullah Dibas
  • 1,499
  • 1
  • 9
  • 13
-1

this worked

    var test=['10.0.0.1','10.0.0.103','10.0.0.111','10.0.0.131','10.0.0.198'];
    for(i=1;i<=254;i++){
            if( ! test.includes('10.0.0.'+i.toString()) ) {
                console.log("adding "+'10.0.0.'+i.toString());
            }
  }     
Shawn
  • 3,031
  • 4
  • 26
  • 53