简体   繁体   English

如何在 JavaScript 中找到两个数组之间的差异?

[英]How to find the differences between two arrays in JavaScript?

I'm a new JavaScript user, but I've been tasked with creating a spreadsheet for my community and it seems I've run into a bump.我是一名 JavaScript 新用户,但我的任务是为我的社区创建一个电子表格,看来我遇到了麻烦。 I've been using Google Scripts along with Google Sheets (so my community can also see the information).我一直在使用 Google Scripts 和 Google Sheets(所以我的社区也可以看到这些信息)。

Basically I have two arrays, both populated with lists of players.基本上我有两个数组,都填充了玩家列表。

  1. My first array is populated with a list of active players, these change every other day.我的第一个数组填充了活跃玩家列表,这些列表每隔一天就会发生变化。 Some people drop, some people get added, others stay on the list.有些人掉线,有些人被添加,有些人留在名单上。

  2. My second array is used to keep track of everyone, so it contains a list of all previous members.我的第二个数组用于跟踪每个人,因此它包含所有以前成员的列表。

My issue: Suppose my second array contained the values "Player1","Player2", and "Player3", and my first array contains the values "Player1","Player2", and "Player3".我的问题:假设我的第二个数组包含值“Player1”、“Player2”和“Player3”,而我的第一个数组包含值“Player1”、“Player2”和“Player3”。

Now consider the change in the first array, perhaps Player1 is removed, and Player 4 is added (in random order), where the second array contains "Player1","Player2", and "Player3", but my first array contains the values "Player4","Player2", and "Player3".现在考虑第一个数组的变化,也许 Player1 被删除了,并添加了 Player 4(以随机顺序),其中第二个数组包含“Player1”、“Player2”和“Player3”,但我的第一个数组包含值“玩家 4”、“玩家 2”和“玩家 3”。 I want a something that compares the two arrays (in any order) and returns the difference between the first array and the second array, and then subsequently adds them onto the second array.我想要一个比较两个数组(以任何顺序)并返回第一个数组和第二个数组之间的差异的东西,然后将它们添加到第二个数组中。

IE: IE:
Array 1 -- Active Members = Player1,Player2,Player3,Player6,Player9,Player4数组 1 -- 活跃成员 = Player1,Player2,Player3,Player6,Player9,Player4
Array 2 -- Total Members = Player1,Player2,Player3,Player4,Player6,Player9数组 2 -- 成员总数 = Player1,Player2,Player3,Player4,Player6,Player9

would return NO difference, but不会返回任何差异,但是

Array 1 -- Active Members = Player1,Player4,Player3数组 1 -- 活跃成员 = Player1,Player4,Player3
Array 2 -- Total Members = Player2,Player3数组 2 -- 成员总数 = Player2,Player3

would return Player1 and Player4, and then add them to Array 2 to make Array 2:将返回 Player1 和 Player4,然后将它们添加到数组 2 以生成数组 2:
Player2,Player3,Player1,Player4玩家 2、玩家 3、玩家 1、玩家 4

I know it's really confusing, but I can't wrap my head around how to get this to work!我知道这真的很令人困惑,但我无法理解如何让它发挥作用!

Here's some pseudo code to demonstrate what I want:这是一些伪代码来演示我想要什么:

if Array1[x] is found in Array2
ignore

but

if Array1[x] is not found in Array2
add the value of [x] to the end of Array2

I'm using Google Scripts to code for a Spreadsheet in Google Spreadsheet here's the basic outline of my code:我正在使用 Google 脚本为 Google 电子表格中的电子表格编码,这是我的代码的基本大纲:

 for (var i=0;i<=range.length;i++){
 var playerName = sheetWAR.getRange(i+4,4).getValue(); // first 3 rows are for information
 }

where range is a value I insert view the Browser.inputBox command.其中 range 是我插入的值,查看 Browser.inputBox 命令。

I then take this data and paste it to another sheet:然后我把这些数据粘贴到另一张纸上:

for (var j=0;j<=playerName.length;j++){
sheetRANK.getRange(j+2,3).setValue(playerName[j]);
}

Good, now I want to take those values and compare them with a list (that will default to empty!很好,现在我想获取这些值并将它们与列表进行比较(默认为空!

var maxRow = sheetRANK.getLastRow();
for (var k=0; k<=maxRow; k++){
var activeRange = sheetRANK.getRange(k,1).getValue();
}

activeRange = Array2活动范围 = Array2
playerNumber = Array1玩家编号 = 数组 1

I've tried both of the suggestions below and here's what happens:我已经尝试了以下两个建议,结果如下:

If I have these values for each array:

activeRange = {"Viper", "Cobra", "Diamondback", "Python"}
and
playerName = {"Viper", "Cobra", "Anaconda", "Diamondback", "Python"}

resulting activeRange = {"Viper", "Cobra", "Diamondback", "Python", "", "", "Anaconda", "undefined"}

for both of the posted solutions.

I failed to realize that I had copied 1 above the playerName into the array, so a simple playerName.length-1 in the for loop solved that issue, the other issue was with a blank space appearing in the compiled list: this is probably the best demonstration of a new-to-JavaScript mistake.我没有意识到我已将 playerName 上方的 1 复制到数组中,因此 for 循环中的一个简单 playerName.length-1 解决了该问题,另一个问题是编译列表中出现了一个空格:这可能是JavaScript 新手错误的最佳演示。 I simply forgot that I was using lastRow as the length, so if I had fewer values in the activeRange (array2) than in playerNum (array1), it would copy down to the last row in array1, which would be a blank space in array2, then once it compiled those two arrays together, with array2 coming before array1, the blank space would separate the two.我只是忘记了我使用 lastRow 作为长度,所以如果我在 activeRange (array2) 中的值比 playerNum (array1) 中的值少,它会复制到 array1 中的最后一行,这将是 array2 中的一个空格,然后一旦将这两个数组编译在一起,array2 在 array1 之前,空格就会将两者分开。

Hope this help:希望这有帮助:

var arr1 = ["Player1", "Player4", "Player3"];
var arr2 = ["Player2", "Player3"];

arr1.forEach(function(player) {
    if(arr2.indexOf(player) == -1) arr2.push(player);
});
console.log(arr2);

// ["Player2", "Player3", "Player1", "Player4"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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