问题描述
我正在使用提供的解决方案 HERE 来比较两个数组.提供的示例将在两个数组中找到的值返回到Array1( same
),仅将在一个或另外两个Array2中找到的值( diff
)返回.
I'm working from the solution provided HERE to compare two arrays. The example provided returns values found in both arrays to Array1 (same
) and values only found on one or the other two Array2 (diff
).
问题::当我将其应用于自己的脚本时, valuesDATA
不返回任何内容,而 valuesCheckSeeding
返回两个数组中的所有值
ISSUE: When I apply it to my own script, valuesDATA
returns nothing and valuesCheckSeeding
returns ALL values from both arrays
期望的结果:我有两个数组,我想从中创建第三个数组,或者只从第一个数组中选择 valuesDATA
的值,这些值不是出现在第二个 valuesCheckSeeding
中.使用上面的解决方案,我试图将所有未在 valuesCheckSeeding
中找到的值和 valuesDATA
推送到 valuesDATA
中.
DESIRED RESULT: I have two arrays that I'd either like to create a third out of, or only select values from the first array, valuesDATA
which are NOT present in the second, valuesCheckSeeding
. Using the solution above, I was trying to have all values not found in valuesCheckSeeding
AND valuesDATA
pushed to valuesDATA
.
valuesDATA
的样本:"U09 F哈福德FC希尔/希利-A医学博士CMSA女孩周六U09 A/B北总理-顶TID0118"
SAMPLE OF valuesDATA
: "U09 FHarford FC Hill/Healey - AMDCMSA Girls Saturday U09 A/B NorthPremier - TopTID0118"
我做错了什么?我修改了循环中的 matchfound == false
和 matchfound = true
,但仍然不能达到预期的效果.
What am I doing wrong? I tinkered with changing matchfound==false
and matchfound=true
in the loop, but that still didn't give me the desired result.
最相关的片段
var matchfound = false;
for (var i = 0; i < valuesDATA.length; i++) {
matchfound=false;
for (var j = 0; j < valuesCheckSeeding.length; j++) {
if (valuesDATA[i] == valuesCheckSeeding[j]) {
valuesCheckSeeding.splice(j, 1);
matchfound=true;
continue;
}
}
if (matchfound==false) {
valuesCheckSeeding.push(valuesDATA[i]);
valuesDATA.splice(i, 1);
i=i-1;
}
}
从下面的评论/答案中编辑的WORKIG脚本
//UPDATE SEEDING SHEET
function updateSeedingSheet() {
var today = Utilities.formatDate(new Date(),Session.getScriptTimeZone(), "MM/dd/yyyy hh:mm a");
//INPUT SHEET INFO
var inputCurrentRow = 4;
var inputCurrentColumn = 20;
var inputNumRows = 1000;
var inputNumColumns =1;
var ssInput = SpreadsheetApp.openById('1Wzg2BklQb6sOZzeC0OEvQ7s7gIQ07sXygEtC0CSGOh4');
var sheetDATA = ssInput.getSheetByName('DATAREF');
var rangeDATA = sheetDATA.getRange(inputCurrentRow, inputCurrentColumn, inputNumRows, inputNumColumns);
var valuesDATA = rangeDATA.getValues();
//SEEDING SHEET INFO
var seedingCurrentRow = 4;
var seedingCurrentColumn = 1;
var seedingNumRows = 1000;
var seedingNumColumns = 1;
var ssSeeding = SpreadsheetApp.openById('1DuCHeZ3zba-nHq-7vYTrylncPGqcA1J9jNyW9DaS3mU');
var sheetSeeding = ssSeeding.getSheetByName('Seeding');
var rangeCheckSeeding = sheetSeeding.getRange(4, 102, 1000, 1);
var columnToClear = sheetSeeding.getRange(seedingCurrentRow, seedingCurrentColumn, seedingNumRows, seedingNumColumns);
var valuesCheckSeeding = rangeCheckSeeding.getValues();
//METHOD TO FILTER
valuesCheckSeeding = valuesCheckSeeding.map(function(e){return e[0];}); //flatten this array
var filteredArr = valuesDATA.filter(function(e){
return !(this.indexOf(e[0])+1);
},valuesCheckSeeding);
Logger.log(filteredArr);
Logger.log(filteredArr.length);
var rangeSeeding = sheetSeeding.getRange(seedingCurrentRow, seedingCurrentColumn, filteredArr.length, seedingNumColumns);
sheetSeeding.getRange('A1').setValue(today);
columnToClear.clearContent();
rangeSeeding.setValues(filteredArr);
/*
//ALTERNATIVE METHOD USING LOOPS
for (var i = 0; i < valuesDATA.length; i++) {
for (var j = 0; j < valuesCheckSeeding.length; j++) {
if (valuesDATA[i][0] == valuesCheckSeeding[j][0]) {
valuesDATA.splice(i, 1);
i--; //account for the splice
break; //go to next i iteration of loop
}
}
}
Logger.log("VALUES DATA:" + valuesDATA);
Logger.log("VALUES CHECK SEEDING: " + valuesCheckSeeding);
//sheetSeeding.getRange('A1').setValue(today);
//rangeSeeding.clearContent();
//rangeSeeding.setValues(valuesDATA); //INCORRECT RANGE HEIGHT, WAS 71 BUT SHOULD BE 1000 - Is splice affecting this?
*/
}//END FUNCTION
推荐答案
V8(ES2016更新):
- 您可以使用更新高效的设置类
const array1 = [[1],[2],[3]],
array2 = [[1],[3],[4]],
set = new Set(array2.flat())
console.info(array1.filter(e => !set.has(e[0])))
//expected output [[2]]
- 您正在检查2D阵列.您需要使用
[i] [0]
和[j] [0]
- 您只能尝试拼接
valuesDATA
尝试
for (var i = 0; i < valuesDATA.length; i++) {
for (var j = 0; j < valuesCheckSeeding.length; j++) {
if (valuesDATA[i][0] == valuesCheckSeeding[j][0]) {
valuesDATA.splice(i, 1);
i--; //account for the splice
break; //go to next i iteration of loop
}
}
}
Logger.log(valuesDATA);
或者,尝试
Alternatively, try
valuesCheckSeeding = valuesCheckSeeding.map(function(e){return e[0];}); //flatten this array
var filteredArr = valuesDATA.filter(function(e){
return !(this.indexOf(e[0])+1);
},valuesCheckSeeding);
Logger.log(filteredArr);
这篇关于Google Apps脚本:比较数组的唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!