1

I have an array of string values. I'd like to loop through the array and return any values that not only match the string values, but contain the values.What would be the best approach to this?

Here is what I have so far, which is searching for the exact value.

I'm using this logic is several places to match the url. For part of the site getLocation returns /about, /services, etc... There is also an admin part which returns /adminabout, /adminservices, etc.

var getLocation = $location.$$path;
var isCustom    = ['about', 'services', 'volunteer', 'contact', 'give', 'blog'];

if(!isCustom.indexOf(getLocation) == -1){ 
  $scope.isCustom = true || false;  
}
6
  • What is the expected output? Commented Mar 19, 2015 at 13:44
  • I've added some additional info Commented Mar 19, 2015 at 13:46
  • I am sorry, its really not clear to me what exactly you are trying to do. What do you expect to happen after this code is executed? Commented Mar 19, 2015 at 13:47
  • If getLocation contains the slash, then it will never match any of those objects. Commented Mar 19, 2015 at 13:48
  • 1
    = false || true - Delightful! You should read it aloud. Commented Mar 19, 2015 at 13:48

2 Answers 2

2

Use a filter to get the matched elements which doesn't contain the getLocation value.

var getLocation = $location.$$path.replace('/','');
var isCustom    = ['about', 'services', 'volunteer', 'contact', 'give', 'blog'];

var matchedResult = isCustom.filter(function(value) {
    return value.indexOf(getLocation) < 0
});

As question somewhat not clear, I assume you're trying to find if a string is present in the array item.

var getLocation =$location.$$path.replace('/', ''); // assuming it to be admin
var isCustom = ["/adminabout","adminservices","about","services"]


// matches if the string is present in any part of the array item
var matchedResult = isCustom.filter(function(value) {
    return value.indexOf(getLocation) !== -1
});

console.log(matchedResult); //["/adminabout", "adminservices"]
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you. Is there a way to loop over each item and see if it contains that value. Similar to string.contains?
that's what we are doing currently. We are loopng through each item and checking if it contains the string
This works well, except in my case the getlocation variable is going to be '/adminabout', with isCustom containing values like 'admin'. I've posted the answer that worked for me.
You just need to replace / initially before checking.
I would still prefer filter instead of manually looping through and pushing.
|
0

I came up with a solution that matches whether or not the values in the isCustom value is contained in the getLocation value.

var searchThroughArray = (function (getLocation) {
var log = [];  
angular.forEach(isCustom, function(item, key) {
      var getLocation = $location.$$path.replace('/', '');
      if(getLocation.contains(item) == true){
        this.push(item);
    }
}, log);
if (0 < log.length){ 
    return true;
};
})();


if(searchThroughArray){
  $scope.isCustom = true; 
}else{
  $scope.isCustom = false; 
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.