user1439488
user1439488

Reputation: 13

Trying to match values in two arrays and only delete if there is an exact match for part of the value

I receive two arrays of IP addresses, which are formatted differently. Any values in the IPs array should be deleted from the addresses array - but only if the IPs match exactly. I've written the below, but the issue is that for example, 192.168.0.1 will match 192.168.0.11 and then subsequently delete 192.168.0.11 from the addresses array which is not a valid result. The addresses array needs to be returned in the same format as it is received. Any help please? :)

var addresses = [{
    Value : '192.168.0.11'
}, {
    Value : '52.210.29.181'
}, {
    Value : '52.210.128.97'
}
];

var IPs = ['192.168.0.1', '52.210.128.97'];

console.log('Before:', addresses);

for (var x = 0; x < IPs.length; x++) {  

for (var key in addresses) {
    var address = JSON.stringify(addresses[key]);

    if (address.indexOf(IPs[x]) > -1){ //if the IP is a substr of address

        console.log('matched, so delete', addresses[key]);
        var index = addresses.indexOf(addresses[key]); //find the index of IP to be deleted then delete it
        addresses.splice(index, 1);

    }


}
}

console.log('After', addresses);

Upvotes: 1

Views: 162

Answers (7)

Sabbir
Sabbir

Reputation: 359

Edit:

ES6 syntax (Array.filter & Array.include & Arrow function)

const addresses = [
  {
Value: "192.168.0.11",
  },
  {
Value: "52.210.29.181",
  },
  {
Value: "52.210.128.97",
  },
];

const IPs = ["192.168.0.1", "52.210.128.97"];

const filterdAddresses = addresses.filter((item) => !IPs.includes(item.Value));

console.log(filterdAddresses);

Original Answer:

Using clean approach of Array.filter :

    var addresses = [{
       Value: '192.168.0.11'
       },
      {
       Value: '52.210.29.181'
      }, {
      Value: '52.210.128.97'
    }];
    
    var IPs = ['192.168.0.1', '52.210.128.97'];
    
    var filterdAddresses = addresses.filter(function (item) {
        var match = false;
        IPs.forEach(function (ip) {
            if (item.Value == ip) {
                match = true;
            }
    
        });
        return !match;
    
    });
    
    console.log(filterdAddresses);

Upvotes: 8

sha-1
sha-1

Reputation: 2093

A minor update to the @Sabbir's approach:

var addresses = [
    {
        Value: '192.168.0.11'
    },
    {
        Value: '52.210.29.181'
    }, 
    {
        Value: '52.210.128.97'
    }];
    
var IPs = ['192.168.0.1', '52.210.128.97'];

var filterdAddresses = addresses.filter(function (item) {
    // If the value exists in IPs array, indexOf will return the index of that value, otherwise it will return -1
    // And if it returns -1 then it didn't match so we return 'true', as we won't filter/remove it
    return (IPs.indexOf(item.Value) == -1);    
});
    
console.log(filterdAddresses); //[ { Value: '192.168.0.11' }, { Value: '52.210.29.181' } ]

Upvotes: 0

10100111001
10100111001

Reputation: 1832

This solution is much more efficient than the others.

var addresses = [{
    Value : '192.168.0.11'
}, {
    Value : '52.210.29.181'
}, {
    Value : '52.210.128.97'
}
];

var IPs = ['192.168.0.1', '52.210.128.97'];

var obj =  {};
addresses.forEach(function(a, i) { obj[a.Value] = i; });
IPs.forEach(function(i) { if (obj[i] != null) addresses.splice(obj[i],1); }); 

console.log(addresses);

Upvotes: 0

Marian07
Marian07

Reputation: 2580

var addresses = [{
    Value: '192.168.0.11'
}, {
    Value: '52.210.29.181'
}, {
    Value: '52.210.128.97'
}];

var IPs = ['192.168.0.1', '52.210.128.97'];


// Loop through IPs
IPs_loop:
for (var ipIndex = 0; ipIndex < IPs.length; ipIndex++) {

    var currentIP = IPs[ipIndex];

    // loop through addresses
    for (var adsIndex = 0; adsIndex < addresses.length; adsIndex++) {

        var currentAds = addresses[adsIndex];
        if (currentAds.Value == currentIP) {
            removeAddressFromIndex(adsIndex);
            break IPs_loop;
        }
    } // end of addresses Loop

} // end of IPs Loop

function removeAddressFromIndex(theReceivedIndex) {
    addresses.splice(theReceivedIndex, 1);
}

console.log(addresses);


( About IPs_loop (label and statement) )

Upvotes: 0

Redu
Redu

Reputation: 26181

I would do this job as follows;

var   ips = ['192.168.0.1', '52.210.128.97'],
addresses = [{Value : '192.168.0.11'},
             {Value : '52.210.29.181'},
             {Value : '52.210.128.97'}
            ],
   result = addresses.map(obj => obj.Value)
                     .filter(ip => !ips.includes(ip));
console.log(result);

Upvotes: 0

Denis Tambovchanin
Denis Tambovchanin

Reputation: 556

Use something like

var res = [];

var addresses = [{
  Value : '192.168.0.11'
}, {
  Value : '52.210.29.181'
}, {
  Value : '52.210.128.97'
}];

var IPs = ['192.168.0.1', '52.210.128.97'];

console.log('Before:', addresses);

addresses.forEach(function(addr) {
  IPs.forEach(function(ip) {
    if (addr.Value === ip) res.push(addr);
  });
});

console.log('After', res);    

In res you will get only

{ Value: '52.210.128.97' }

Upvotes: -1

kevin ternet
kevin ternet

Reputation: 4612

I propose a forEach and a splice like this :

addresses.forEach((item, index, arr) => {if (IPs.indexOf(item.Value) != -1) arr.splice(index,1)});

console.log(addresses); //[ { Value: '192.168.0.11' }, { Value: '52.210.29.181' } ]

Upvotes: 0

Related Questions