0

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.

7 Answers 7

1

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);

Sign up to request clarification or add additional context in comments.

1 Comment

Nice one, didn't know Set sound's cool, I will take a look :)
0

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)

Comments

0

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.

Comments

0

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.');
}

Comments

0

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;
         }
    );
}

Comments

0

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)

Comments

0

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`);

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.