Compare array with sorted array, select first element

The setup is as follows:

targets = ['green','orange','red'];  //targets are in order of priority
sources = ['redalert','blackadder','greenlantern'];

      

I am trying to create a function that returns a single source containing the highest priority row. In this case, it will be "greenlantern", since it contains the string "green", which has a higher priority than "red" found in "redalert".

I've already used it for loops and temp arrays, but I know these manipulations are not my strong point and my real arrays are much larger, so I would like to optimize execution. I tried Lodash too, but can't figure out how to do it all in one step. Is it possible?

As I see it, it should:

  • for each target, loop through sources, if source elem matches target elem, break and return.

but i'm sure there is a better way.

+3


source to share


2 answers


Here's another lodash approach that uses reduce () instead of sortBy () :

_.reduce(targets, function(result, target) {
    return result.concat(_.filter(sources, function(source) {
        return _.includes(source, target);
    }));
}, []);

      

Since it's targets

already in order, you can iterate over it and build the result in the same order. You are using reduce()

because you are creating the result iteratively, which is not a direct mapping.

Inside the decrease callback, you can concat()

get the results with filter () and include () to find the matching one sources

.




This gives you a sorted array, but it also does a lot of unnecessary work if you only want the first one source

that matches the first target

:

_.find(sources, _.ary(_.partialRight(_.includes, _.first(targets)), 1));

      

Or, if you prefer not to create callback functions:

_.find(sources, function(item) {
    return _.includes(item, _.first(targets));
});

      

Essentially find () will only iterate over the collection sources

until a match is found. The first () function gives you the first one target

to search.

+1


source


Saving is very simple:

var sortedSources = _.sortBy(sources, function(source){
  var rank = 0
  while(rank < targets.length){
    if(source.indexOf(targets[rank]) > -1){
      break
    }else{
      rank++
    }
  }
  return rank
})

      



Sources are now sorted by target priority, thus sortedSources[0]

your person.

+1


source







All Articles