0

I'm having a tough time figuring out how to loop through an array and if certain items do exist within the array, i'd like to perform a .slice(0, 16) to kind of filter an already existing array (lets call that existing array "routes").

For example, a previous process will yield the following array:

points = ['=00ECY20WA200_RECV_P1SEL',
'=00ECY20WA200_RECV_P2SEL',
'=00RECV_C1A_EINCSCMPP1',
'=00RECV_C1A_EINCSCMPP2',
'=00BYPS_C1A_EINCSCMP',
'=00ECY20WA200_BYPS_SPSL1',
'=00ECC92AG184YB01',
'=00ECC92AG185YB01',
'=00ECC92AG186YB01',
'=00ECC92AG187YB01',
]

So if any of the above items exist in the "points" Array, which in this case they all do (but in some cases it could just be 1 of the 10 items existing there), I'm trying to perform routes.slice(0, 16) to the other already existing array.

I've tried lots of different ways (for loops with if statements) and at this point I'm not sure if its my syntax or what, but I'm back at square 0 and I don't even have a competent piece of code to show for. Any direction would be greatly appreciated.

9
  • 1
    are you trying to check if a string exists in points and then pull that string out and put it in another array? Commented Jan 30, 2017 at 14:26
  • Have you looked into Array.prototype.filter? Commented Jan 30, 2017 at 14:26
  • So you want to have the data in one array (points) filter data from another array (routes)? Commented Jan 30, 2017 at 14:26
  • you say you want to perform a slice from index 0 to 16, is one or both of those values deterministic of whats found in the array? It's hard to get the context of what you want form your question. Commented Jan 30, 2017 at 14:27
  • Apologies, re-reading this now, I can see it's a bit confusing. What I'm trying to do is look at the array "points", if "points" has any of the items listed in my example, I want to perform slice() on another array. If none of the items exist in "points", matching up with the items in my example, I want it to do nothing. Hope that's clearer. Commented Jan 30, 2017 at 14:36

3 Answers 3

1

You could use a hash table for checking and filtering.

var points = ['=00ECY20WA200_RECV_P1SEL', '=00ECY20WA200_RECV_P2SEL', '=00RECV_C1A_EINCSCMPP1', '=00RECV_C1A_EINCSCMPP2', '=00BYPS_C1A_EINCSCMP', '=00ECY20WA200_BYPS_SPSL1', '=00ECC92AG184YB01', '=00ECC92AG185YB01', '=00ECC92AG186YB01', '=00ECC92AG187YB01'], 
    hash = Object.create(null),
    filtered = points.filter(function (a) {
        if (!hash[a.slice(0, 16)]) {
            hash[a.slice(0, 16)] = true;
            return true;
        }
    });
              
console.log(filtered);                 

ES6 with Set

var points = ['=00ECY20WA200_RECV_P1SEL', '=00ECY20WA200_RECV_P2SEL', '=00RECV_C1A_EINCSCMPP1', '=00RECV_C1A_EINCSCMPP2', '=00BYPS_C1A_EINCSCMP', '=00ECY20WA200_BYPS_SPSL1', '=00ECC92AG184YB01', '=00ECC92AG185YB01', '=00ECC92AG186YB01', '=00ECC92AG187YB01'], 
    pSet = new Set,
    filtered = points.filter(a => !pSet.has(a.slice(0, 16)) && pSet.add(a.slice(0, 16)));
              
console.log(filtered);                 

Sign up to request clarification or add additional context in comments.

Comments

1

EDIT: So it seems like you want to remove an element from an array called routes for each element in the points array. This is how you could do this:

function removeBrokenRoutes(brokenPoints, routes){
    for(let pt of brokenPoints){
        let index = routes.indexOf(pt);
        if(index !== -1) routes.splice(index,1);
    }
    return routes;
}

Keep in mind that the larger the arrays, the more time this is going to take to complete.

Comments

0

You could use the filter and indexOf methods in combination:

var arr = [/* all the data you're checking against */];
var points = [/* the data you're checking for */];
var filteredArr = arr.filter(function(x) {
     // will return -1 if the point is not found
     return points.indexOf(x) !== -1;
});

filteredArr will contain all the points that appear in both arrays. The filter function works by taking a function with one argument x, which represents each item in the array. if the function returns true, the item will be added to the new array (filteredArr), and if false the function will move on to the next item. indexOf will check if the item is found in the other array. Also it is important to note that you will need a more complex solution (such as a hashtable) if the data set is very, very large as this is not necessarily the most performant method. But it's a good place to start as it is easy to understand.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.