Data fuzzy matching two arrays, the arrays have the same filter element

demand:

var imgArr = [ 'Audi', 'BMW', 'Beijing Automotive', 'Mercedes',' BYD ',' public ',' wind ',' lucky ',' Gold ',' Renault ',' Chery ',' Skoda ',' Teng potential '];

var carName = [ 'Aeolus A60 east', 'Imperial lucky', 'BAIC Saab', 'Chery eQ', 'Volkswagen Passat'];

was carTypeImg = [];

for(var a = 0; a < carName.length; a++){		    
   for (var i = 0; i < imgArr.length; i++) {
		if(carName[a].indexOf(imgArr[i]) != -1) {
			this.carTypeImg.unshift(imgArr[i]);					
		}
	}
}

 

to sum up:

1. There are two elements of the same array, a new array selected carTypeImg.

2. Screening of two ways:

① using indexOf () This method to find the position of the same element, if not equal to minus one, the same elements described can be excised into a new array, as in FIG.

//字符串方法indexOf
var len = carName.length;
var arr = [];
for(var i=0;i<len;i++){
    //如果字符串中不包含目标字符会返回-1
    if(carName[i].indexOf(keyWord)>=0){
        arr.push(carName[i]);
    }
}
return arr;

② The regular match

ar len = carName.length;
var arr = [];
var reg = new RegExp(keyWord);
for(var i=0;i<len;i++){
    //如果字符串中不包含目标字符会返回-1
    if(carName[i].match(reg)){
        arr.push(carName[i]);
    }
}
return arr;

 

Guess you like

Origin blog.csdn.net/weixin_28898107/article/details/94739571