[英]Javascript compare two arrays and get values that do not match
I really appreciate all your help on this. 非常感谢您在此方面的所有帮助。
I have two arrays: 我有两个数组:
I need to output a third array, in this case FinalArray
that contains a list of all the ArrayFileNameWExt
that are not in the ArrayFileName
array. 我需要输出第三个数组,在这种情况下,
FinalArray
包含不在ArrayFileName
数组中的所有ArrayFileNameWExt
的列表。
I know I had a thread on finding matched items, which was great. 我知道我在寻找匹配项上有个思路,这很棒。 But I'm having problems finding the unmatched items.
但是我在寻找不匹配的物品时遇到了问题。 I changed the == comparison to !== and that gave me one file name a hundred times.
我将==比较更改为!==,这给了我一个文件名一百次。
Thank you for your help on this, Maxine 谢谢您对此的帮助,Maxine
var ArrayFileName = ['one', 'two', 'three', 'three', 'five', 'six', 'ten'];
var ArrayFileNameWExt = ['one.txt', 'two.txt', 'three.txt', 'ten.wmf', 'eleven.cgm'];
var FinalArray = [];
for (var i = 0; i < ArrayFileName.length; i++) {
for (var j = 0; j < ArrayFileNameWExt.length; j++) {
var temp = ArrayFileNameWExt[j].split(".");
if(ArrayFileName[i]!==temp[0]){
FinalArray.push(ArrayFileNameWExt[j]);
break;
}
}
}
You could use a simple filter and return all the items for which the first part of the split is not in the ArrayFileName
array. 您可以使用简单的过滤器,并返回所有拆分第一部分不在
ArrayFileName
数组中的项。
var ArrayFileName = ['one', 'two', 'three', 'three', 'five', 'six', 'ten']; var ArrayFileNameWExt = ['one.txt', 'two.txt', 'three.txt', 'ten.wmf', 'eleven.cgm']; var final = ArrayFileNameWExt.filter(function(item) { return !ArrayFileName.includes(item.split('.')[0]); }) console.log(final);
If you're using a pretty old version of javascript, the includes
Array method might not exist. 如果您使用的是JavaScript的较旧版本,则
includes
Array方法可能不存在。 The following code could be used instead. 可以使用以下代码代替。
var ArrayFileName = ['one', 'two', 'three', 'three', 'five', 'six', 'ten']; var ArrayFileNameWExt = ['one.txt', 'two.txt', 'three.txt', 'ten.wmf', 'eleven.cgm']; var final = ArrayFileNameWExt.filter(function(item) { var name = item.split('.')[0]; for (var i = 0; i < ArrayFileName.length; i++) { if (ArrayFileName[i] === name) return false; } return true; }) console.log(final);
Reusing the code you gave us, I made this: I interchanged the two for-loops and I used a variable ( found
) to keep track of the found items. 重用您提供给我们的代码,我做到了:我互换了两个for循环,并使用了一个变量(
found
)来跟踪找到的项目。 Iterating through ArrayFileNameWExt
in the outer loop is also better because we lower the number of calls to the split
function. 在外部循环中遍历
ArrayFileNameWExt
也更好,因为我们减少了对split
函数的调用次数。
var ArrayFileName = ['one', 'two', 'three', 'three', 'five', 'six', 'ten']; var ArrayFileNameWExt = ['one.txt', 'two.txt', 'three.txt', 'ten.wmf', 'eleven.cgm']; var FinalArray = []; for (var i = 0; i < ArrayFileNameWExt.length; ++i) { var temp = ArrayFileNameWExt[i].split("."); var found = false; for (var j = 0; j < ArrayFileName.length; ++j) { if (ArrayFileName[j] === temp[0]) { found = true; break; } } if (!found) { FinalArray.push(ArrayFileNameWExt[i]); } } console.log(FinalArray);
Cheers! 干杯!
I would use .filter
and .includes
- 我会使用
.filter
和.includes
const fileNames = ['one', 'two', 'three', 'three', 'five', 'six', 'ten'] const fileNamesWExt = [ 'one.txt', 'two.txt', 'three.txt', 'ten.wmf', 'eleven.cgm' ] const basename = filename => filename.split(/\\.[^.]+$/)[0] const finalArray = fileNamesWExt.filter(f => ! fileNames.includes(basename(f))) console.log(finalArray) // [ 'eleven.cgm' ]
Note our implementation of basename
, which is careful to only remove the last extension - 请注意我们对
basename
的实现,注意仅删除最后一个扩展名-
const basename = filename => filename.split(/\\.[^.]+$/)[0] console.log ( basename ("cat") // "cat" , basename ("cat.dog") // "cat" , basename ("cat.dog.eel") // "cat.dog" , basename ("cat.dog.eel.fox") // "cat.dog.eel" )
If you cannot use arrow functions, you are probably using a pretty old version of JavaScript. 如果您不能使用箭头功能,则可能使用的是JavaScript的旧版本。 In this case, you'll also need to polyfill
.includes
- 在这种情况下,您还需要polyfill
.includes
Array.prototype.includes = function (x, init) { for (var i = init || 0; i < this.length; i = i + 1) if (this[i] === x) return true; return false; }; var fileNames = ['one', 'two', 'three', 'three', 'five', 'six', 'ten']; var fileNamesWExt = [ 'one.txt', 'two.txt', 'three.txt', 'ten.wmf', 'eleven.cgm' ]; var basename = function (filename) { return filename.split(/\\.[^.]+$/)[0]; }; var finalArray = fileNamesWExt.filter(function (f) { return ! fileNames.includes(basename(f)); }); console.log(finalArray); // [ 'eleven.cgm' ]
Perhaps Editor would like this 也许编辑会喜欢这样
var ArrayFileName = ['one', 'two', 'three', 'three', 'five', 'six', 'ten']; var ArrayFileNameWExt = ['one.txt', 'two.txt', 'three.txt', 'ten.wmf', 'eleven.cgm']; var FinalArray = []; for (var i = 0; i < ArrayFileNameWExt.length; i++) { var matchFound = false; for (var j = 0; j < ArrayFileName.length; j++) { var temp = ArrayFileNameWExt[i].split("."); if(ArrayFileName[j]==temp[0]){ matchFound = true; break; } } if(!matchFound){ FinalArray.push(ArrayFileNameWExt[i]) } } console.log(FinalArray)
Array#filter
lets you include (or delete) the elements that meet certain condition. Array#filter
允许您包含(或删除)满足某些条件的元素。
Array#inludes
returns if certain element is included in the array. 如果
Array#inludes
某些元素,则Array#inludes
返回。
String#split
returns an array with the elements from splitting the string using the separator that you want (in this case, the point). String#split
返回一个数组,其中包含使用所需的分隔符(在本例中为点)分割字符串的元素。
var ArrayFileName = ['one', 'two', 'three', 'three', 'five', 'six', 'ten']; var ArrayFileNameWExt = ['one.txt', 'two.txt', 'three.txt', 'ten.wmf', 'eleven.cgm']; var FinalArray = []; FinalArray = ArrayFileNameWExt.filter (filenameWExt => !ArrayFileName.includes(filenameWExt.split('.')[0])); console.log(FinalArray);
Any further question let me know in the comments and I'll explain 如有任何其他疑问,请在评论中让我知道,我将进行解释
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.