简体   繁体   中英

Matching string with multilayered array

How can i match variable car with variable array? I need to match every first item (Buick, Mercedes, Chevrolet) with my string. I this case should be logged Buick and Chevrolet:

var car = "Buick, Chevrolet";
var array = [
  ["Buick", "2012", "USA", "1201"],
  ["Mercedes", "2005", "Germany", "12354"],
  ["Chevrolet", "1974", "USA", "9401"]
];


if (car = array) {
  console.log("Buick and Chevrolet matches.");  
};

But string could be different - sometimes may be matched items 0, sometimes 30 and so on.

In modern Javascript you use Set and .filter for this:

 var car = "Buick, Chevrolet"; var array = [ ["Buick", "2012", "USA", "1201"], ["Mercedes", "2005", "Germany", "12354"], ["Chevrolet", "1974", "USA", "9401"] ]; var searchCars = new Set(car.match(/\\w+/g)); var found = array.filter(([name]) => searchCars.has(name)); console.log(found); 

You could split the string and iterate for every element and check against the array.

 var car = "Buick, Chevrolet", array = [["Buick", "2012", "USA", "1201"], ["Mercedes", "2005", "Germany", "12354"], ["Chevrolet", "1974", "USA", "9401"]], match = car.split(', ').every(function (a) { return array.some(function (b) { return a === b[0]; }); }); console.log(match); 

 var car = "Buick, Chevrolet", array = [["Buick", "2012", "USA", "1201"], ["Mercedes", "2005", "Germany", "12354"], ["Chevrolet", "1974", "USA", "9401"]], match = car.split(', ').every(function (a) { return array.some(function (b) { return a === b[0]; }); }); if (match) { console.log(car.split(', ').join(' and ') + ' matches.'); } 

You can use reduce() check for element with indexOf()

 var car = "Buick, Chevrolet"; var array = [ ["Buick", "2012", "USA", "1201"], ["Mercedes", "2005", "Germany", "12354"], ["Chevrolet", "1974", "USA", "9401"] ]; var result = array.reduce(function(r, e, i) { if (car.indexOf(e[0]) != -1) { r.push(e[0]); } return r; }, []).join(' and ') + ' matches.'; console.log(result) 

You can use Array.reduce here

 var car = "Buick, Chevrolet"; var array = [ ["Buick", "2012", "USA", "1201"], ["Mercedes", "2005", "Germany", "12354"], ["Chevrolet", "1974", "USA", "9401"] ]; var matches = array.reduce(function(result, item){ if(car.indexOf(item[0]) > -1) result.push(item); return result }, []); console.log(matches) 

You will have to loop over array and you can check for match using string.indexOf()

 var car = "Buick, Chevrolet"; var array = [ ["Buick", "2012", "USA", "1201"], ["Mercedes", "2005", "Germany", "12354"], ["Chevrolet", "1974", "USA", "9401"] ]; var matches = []; var matchesStr = ""; array.forEach(function(item){ if(car.indexOf(item[0]) > -1){ matches.push(item[0]); } }); if(matches.length > 0){ var _last = matches.pop(); matchesStr = matches.toString() + " and " + _last; } else{ matchesStr = matches.toString() } console.log(matchesStr) 

Also, if you have option to update your structure, its better to have array of object instead of array of arrays.

Using array operations filter and map you can produce the desired filtering. First filter the array to get only elements that match the search criteria. Then I map it to extract only data that is required for further operation.

var string = "Buick, Chevrolet";
var cars = [
  ["Buick", "2012", "USA", "1201"],
  ["Mercedes", "2005", "Germany", "12354"],
  ["Chevrolet", "1974", "USA", "9401"]
];

var findCars= function( arr, str ){
   return arr.filter(
         function(s){
              return str.indexOf(s[0])!==-1;
         }
    ).map(
       function(x){
         return x[0];
       }
    );
}

var res = findCars(cars, string);
console.log( res.join(" and ") + " matches.")

If you need the whole matching data item later, then you should skip the mapping of the result and findCars method will look like this:

var findCars= function( arr, str ){
   return arr.filter(
         function(s){
              return str.indexOf(s[0])!==-1;
         }
    );
}

 var car = "Buick, Chevrolet"; var array = [ ["Buick", "2012", "USA", "1201"], ["Mercedes", "2005", "Germany", "12354"], ["Chevrolet", "1974", "USA", "9401"] ]; var result = car .split(', ') .reduce( (res, car) => (array.some(([arrayCar]) => car === arrayCar ? res.push(car) : null), res), []); console.log(`${result.join(' and ')} matches`); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM