Javascript在数组中循环搜索字符
Javascript loop through an array searching for characters
我有一个字符串值数组。我想循环遍历数组,并返回任何不仅与字符串值匹配,而且包含这些值的值。解决这一问题的最佳方法是什么?
这是我目前所掌握的,正在寻找确切的价值。
我使用这个逻辑是在几个地方匹配url。对于站点的一部分,getLocation返回/about、/services等。。。还有一个admin部分,返回/adminabout、/adminservices等。
var getLocation = $location.$$path;
var isCustom = ['about', 'services', 'volunteer', 'contact', 'give', 'blog'];
if(!isCustom.indexOf(getLocation) == -1){
$scope.isCustom = true || false;
}
使用过滤器获取不包含getLocation值的匹配元素。
var getLocation = $location.$$path.replace('/','');
var isCustom = ['about', 'services', 'volunteer', 'contact', 'give', 'blog'];
var matchedResult = isCustom.filter(function(value) {
return value.indexOf(getLocation) < 0
});
由于问题有些不清楚,我假设您正在尝试查找数组项中是否存在字符串。
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"]
我提出了一个解决方案,可以匹配isCustom
值中的值是否包含在getLocation
值中。
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;
};
相关文章:
- 在搜索中考虑特殊字符的
- 正则表达式搜索一个空格,后跟一个字符
- 在字符串列表中搜索任何出现的单词,如果单词的第一个字符匹配,则高亮匹配单词.Javascript正则表达式
- Javascript在数组中循环搜索字符
- 如何在jQuery中搜索两个字符之间的字符串
- 代码点火器:搜索自动完成 JSON 解析意外字符
- 在另一个由非字母数字字符分隔的字符串中搜索(任何)字符串
- JavaScript-使用indexOf()在字符串中搜索字符
- Regex,它搜索一个起始字符,介于两者之间的任何字符,然后是一个结束字符
- 按第一个字符进行数组搜索
- jQuery自动完成下拉列表中的粗体搜索文本字符
- 如何使用javascript/jquery/c#搜索和选择包含特殊字符的文本
- 如何在输入中键入4个字符后在AngularJS中搜索
- 正则表达式搜索短语,在短语后返回“n”个字符
- Js 在具有多语言字符的对象中搜索
- 如何使用nodejs在mongodb集合中搜索重音字符
- PHP/html:即时搜索功能:无法搜索外语(例如中文)字符
- 如何在 angularjs 中为特殊字符应用搜索过滤器
- Twitter Bootstrap 2.3 Typeahead 搜索元素名称与字符和在谷歌浏览器中
- 建议:搜索标记内的拳头字符