如何将变量汽车与变量阵列相匹配?我需要将每个第一项(别克,梅赛德斯,雪佛兰)与我的琴弦相匹配。我这个案子应该记录别克和雪佛兰:
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.");
};
但字符串可能不同 - 有时可能匹配项目0,有时可能是30,依此类推。
答案 0 :(得分:1)
在现代Javascript中,您可以使用Set
和.filter
:
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);

答案 1 :(得分:0)
您可以拆分字符串并迭代每个元素并检查数组。
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.');
}

答案 2 :(得分:0)
您可以reduce()
使用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)

答案 3 :(得分:0)
您可以在此使用Array.reduce
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)
答案 4 :(得分:0)
您必须循环数组,然后使用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)
此外,如果您可以选择更新结构,那么最好使用对象数组而不是数组数组。
答案 5 :(得分:0)
使用数组操作过滤器和贴图,您可以生成所需的过滤。首先过滤数组以仅获取与搜索条件匹配的元素。然后我映射它以仅提取进一步操作所需的数据。
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.")
如果您以后需要整个匹配数据项,那么您应该跳过结果的映射,findCars方法将如下所示:
var findCars= function( arr, str ){
return arr.filter(
function(s){
return str.indexOf(s[0])!==-1;
}
);
}
答案 6 :(得分: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`);