将数组值匹配到 Return 所有匹配的事件
Match Arrays Value To Return All Matched Occurrences
我在两个不同长度数组中的记录匹配方面几乎不需要帮助。
要求:
- 查找两个数组之间的相同值
- Return 只有匹配的
- 将匹配的值推回找到值的数组。
示例:
var findFrom = ["Apple", "Mango", "Orange", "Mango", "Mango", "Apple"];
var findTheseValues = ["Apple", "Mango"];
Returned Array = ["Apple", "Apple("Matched")", "Orange", "Mango", "Mango("Matched")", "Mango", "Mango(Matched)", "Apple("Matched")"];
**// Push matched values next to the value that was matched in the FindFrom Array**
我试过了:
var findFrom = ["Apple", "Mango", "Orange", "Banana", "Orange", "Orange","Orange"];
var findTheseValues = ["Orange", "Banana"];
for(let i = 0; i < findFrom.length; i++){
if (findFrom[i] === findTheseValues[i] // toString() if required){
console.log(findFrom[i]);
}
}
如果我只是将查找这些值的 if 条件中的 'i' 替换为 0,它 returns 匹配的值,但我不希望它只匹配一个值 - 它应该遍历两个数组。
尝试从 ES 6 中查找,但只有 returns 一个匹配的值。
如果需要,我很乐意解释更多,感谢您的帮助! :)
您可以使用 Arrays 映射方法,该方法return 一组附加了匹配文本的新数组。
var returnedArray = findFrom.map((ele)=> {
if(findTheseValues.indexOf(ele)!==-1){
return [ele, ele+"(matcheed)"] // return a pair with appended matched
}else{
return ele;
}
}).join().split(',') // to flatten array.
这个解决方案对我有用:
var findFrom = ["Apple", "Mango", "Orange", "Mango", "Mango", "Apple"];
var findTheseValues = ["Apple", "Mango"];
var solution = [];
for(let i = 0; i < findFrom.length; i++){
solution.push(findFrom[i]);
if (findTheseValues.indexOf(findFrom[i]) !== -1) {
solution.push(findFrom[i] + ' (Matched)');
}
}
console.log(solution)
这将遍历数组,尝试在 "findthesevalues" 中的索引处找到元素 - 如果找到匹配项,则将 "matched" 字符串推送到新数组
您可以使用 .map()
和 .includes()
方法:
let findFrom = ["Apple", "Mango", "Orange", "Mango", "Mango", "Apple"],
findTheseValues = ["Apple", "Mango"];
let result = findFrom.map(s => s + (findTheseValues.includes(s) ? ` (${s})` : ''));
console.log(result);
文档:
这里有一个不错的解决方案,它使用本机 Array 方法而不是 for 循环,恕我直言,这总是一个更好的主意。
另外,这 也 允许您多次查找相同的值,例如在您的搜索中多次包含一个值。
let findFrom = ["Apple", "Mango", "Orange", "Mango", "Mango", "Apple"];
// try either of the following
let findTheseValues = ["Apple", "Mango"];
//let findTheseValues = ["Apple", "Mango", "Mango"];
let results = [];
findFrom.forEach((i) => {
const match = findTheseValues.filter((a) => i === a);
const result = match.length > 0 ? match.concat(i) : i;
results.push(i);
if (match.length > 0) {
match.forEach(m => results.push(m));
};
});
console.log(results);
我会做这样的事情
var findFrom = ["Apple", "Mango", "Orange", "Mango", "Mango", "Apple"];
var findTheseValues = ["Apple", "Mango"];
var matches = findFrom.map((e, i)=>{
return r = findTheseValues.includes(e) ? e + '(matched)' : e;
});
console.log(matches);
我写这个答案是因为提出的所有其他解决方案都是基于对 findTheseValues
数组的线性搜索,而使用不同的数据结构可以肯定地降低总计算复杂度:我说的是 Set
s 或 Map
s。它们可能是使用散列 table 实现的,正如 ECMAScript 标准所说:
must be implemented using either hash tables or other mechanisms that, on average, provide access times that are sublinear on the number of elements in the collection.
因此,我的解决方案几乎与其他解决方案相同,除了使用的数据结构。
let findTheseValuesSet = new Set(findTheseValues);
let newArray = [];
findFrom.forEach(elem => {
newArray.push(elem);
if (findTheseValuesSet.has(elem))
newArray.push(elem + '(Matched)');
});
我在两个不同长度数组中的记录匹配方面几乎不需要帮助。
要求:
- 查找两个数组之间的相同值
- Return 只有匹配的
- 将匹配的值推回找到值的数组。
示例:
var findFrom = ["Apple", "Mango", "Orange", "Mango", "Mango", "Apple"];
var findTheseValues = ["Apple", "Mango"];
Returned Array = ["Apple", "Apple("Matched")", "Orange", "Mango", "Mango("Matched")", "Mango", "Mango(Matched)", "Apple("Matched")"];
**// Push matched values next to the value that was matched in the FindFrom Array**
我试过了:
var findFrom = ["Apple", "Mango", "Orange", "Banana", "Orange", "Orange","Orange"];
var findTheseValues = ["Orange", "Banana"];
for(let i = 0; i < findFrom.length; i++){
if (findFrom[i] === findTheseValues[i] // toString() if required){
console.log(findFrom[i]);
}
}
如果我只是将查找这些值的 if 条件中的 'i' 替换为 0,它 returns 匹配的值,但我不希望它只匹配一个值 - 它应该遍历两个数组。
尝试从 ES 6 中查找,但只有 returns 一个匹配的值。
如果需要,我很乐意解释更多,感谢您的帮助! :)
您可以使用 Arrays 映射方法,该方法return 一组附加了匹配文本的新数组。
var returnedArray = findFrom.map((ele)=> {
if(findTheseValues.indexOf(ele)!==-1){
return [ele, ele+"(matcheed)"] // return a pair with appended matched
}else{
return ele;
}
}).join().split(',') // to flatten array.
这个解决方案对我有用:
var findFrom = ["Apple", "Mango", "Orange", "Mango", "Mango", "Apple"];
var findTheseValues = ["Apple", "Mango"];
var solution = [];
for(let i = 0; i < findFrom.length; i++){
solution.push(findFrom[i]);
if (findTheseValues.indexOf(findFrom[i]) !== -1) {
solution.push(findFrom[i] + ' (Matched)');
}
}
console.log(solution)
这将遍历数组,尝试在 "findthesevalues" 中的索引处找到元素 - 如果找到匹配项,则将 "matched" 字符串推送到新数组
您可以使用 .map()
和 .includes()
方法:
let findFrom = ["Apple", "Mango", "Orange", "Mango", "Mango", "Apple"],
findTheseValues = ["Apple", "Mango"];
let result = findFrom.map(s => s + (findTheseValues.includes(s) ? ` (${s})` : ''));
console.log(result);
文档:
这里有一个不错的解决方案,它使用本机 Array 方法而不是 for 循环,恕我直言,这总是一个更好的主意。
另外,这 也 允许您多次查找相同的值,例如在您的搜索中多次包含一个值。
let findFrom = ["Apple", "Mango", "Orange", "Mango", "Mango", "Apple"];
// try either of the following
let findTheseValues = ["Apple", "Mango"];
//let findTheseValues = ["Apple", "Mango", "Mango"];
let results = [];
findFrom.forEach((i) => {
const match = findTheseValues.filter((a) => i === a);
const result = match.length > 0 ? match.concat(i) : i;
results.push(i);
if (match.length > 0) {
match.forEach(m => results.push(m));
};
});
console.log(results);
我会做这样的事情
var findFrom = ["Apple", "Mango", "Orange", "Mango", "Mango", "Apple"];
var findTheseValues = ["Apple", "Mango"];
var matches = findFrom.map((e, i)=>{
return r = findTheseValues.includes(e) ? e + '(matched)' : e;
});
console.log(matches);
我写这个答案是因为提出的所有其他解决方案都是基于对 findTheseValues
数组的线性搜索,而使用不同的数据结构可以肯定地降低总计算复杂度:我说的是 Set
s 或 Map
s。它们可能是使用散列 table 实现的,正如 ECMAScript 标准所说:
must be implemented using either hash tables or other mechanisms that, on average, provide access times that are sublinear on the number of elements in the collection.
因此,我的解决方案几乎与其他解决方案相同,除了使用的数据结构。
let findTheseValuesSet = new Set(findTheseValues);
let newArray = [];
findFrom.forEach(elem => {
newArray.push(elem);
if (findTheseValuesSet.has(elem))
newArray.push(elem + '(Matched)');
});