簡體   English   中英

從不同列中具有雙重標准的二維數組中獲取行號

[英]Get row number from two dimensional array with double criteria in different column

我有一個像這樣的二維數組:

     Name    ID    Date
0    Jhon     2      3
1    Elton    5      6
2    Gonza    8      9
3    Elton    6      1    

(讓我們調用 PrincipalArray 這個數據)

我在 Google Sheet 中有這個數組。 我在 Google Apps Script 中的 JavaScript 中將此值設為二維數組。

然后我想過濾類似:“名稱為 Elton 且 ID 為 6 的行數”-> output 為“第 3 行”

我需要這個,因為當條件為真時我需要編輯這一行並且我需要知道行數。 如果不存在符合這個標准的東西,當 indexof 找不到東西時,我需要一個像“-1.00”這樣的答案。

我會多次重復這個搜索,我的意思是,我需要這個自動化。

我在考慮類似 IndexOf 的東西,但我可以只為 Elton 的第一列提供 IndexOf 並且不存在並且“具有兩個條件的 indexOf”。

在偽代碼中我也這么認為:

  1. 獲取具有名稱的第一列(新的一維數組“NamesArray”)
  2. 獲取帶有 ID 的第 2 列(新的一維數組“IDsArray”)
  3. 過濾數組以了解數組中有多少埃爾頓(我的 For 循環的長度)
  4. 以“數組中有多少eltons”作為長度循環
  5. 在“elton”的第一列中創建 indexof( NamesArray.indexOf("Elton")
  6. 使用 Elton 中 Indexof 的結果來驗證第二列是否在該行中具有該 ID
  7. 迭代..

 function asdasdasd() { //My data in google sheets var PrincipalArray = [ [ "Name", "IDs", "Date"], [ "Jhon", 2, 3 ], ["Elton", 5, 6 ], ["Gonza", 8, 9 ], ["Elton", 6, 1 ], ]; //My first criteria to search var NameToSearch = "Elton"; //will be an input variable by human var IDsToSearch = "6"; //will be an input variable by human //Name Get columns from 2 dimentional array because IndexOf only works in 1 dimentional array var getColumns = (arr, indices) => arr.map(row => indices.map(i => row[i])); var PrincipalArray_Col_Name_2darr = getColumns(PrincipalArray, [0]); var PrincipalArray_Col_Name = PrincipalArray_Col_Name_2darr.map( function(r) {return r[0];}); //IDs Get columns from 2 dimentional array because IndexOf only works in 1 dimentional array var getColumns = (arr, indices) => arr.map(row => indices.map(i => row[i])); var PrincipalArray_Col_IDs_2darr = getColumns(PrincipalArray, [1]); var PrincipalArray_Col_IDs = PrincipalArray_Col_IDs_2darr.map( function(r) {return r[0];}); //Filter the principal array then i can notice how much "Elton" there are var PrincipalArray_OnlyName = PrincipalArray.filter(function(item){ return (item[0] == NameToSearch); } ); //output expected is 2 rows with Elton Data //i will start the loop in the faster position var positionNow = PrincipalArray_Col_Name.indexOf(NameToSearch); //output expected is 2, the first appear of Elton //I will loop across the PrincipalArray but i will use IndexOf to go faster for ( var i=positionNow; i<PrincipalArray_OnlyName.length; i++) { //if is equal to "6" the ID of Elton if ( PrincipalArray_Col_IDs[positionNow] == IDsToSearch ){ //i will edit that ID and run something here becuase is a match Logger.log(PrincipalArray_Col_IDs[positionNow]); break; //break because i found the ID with Elton, i edited that and is enough no more things to do and i want go outside of the loop for } var positionNow = PrincipalArray_Col_Name.indexOf(NameToSearch,positionNow); //output expected is 4 now becuase i am making an indexof starting in the previous step and i will find the next match of "elton" if ( positionNow == PrincipalArray_OnlyName.length ) { //here i will edit something becuase the ID was not found and i need to know that. } } }

使用findIndex()

 //My data in google sheets var PrincipalArray = [ [ "Name", "IDs", "Date"], [ "Jhon", 2, 3 ], ["Elton", 5, 6 ], ["Gonza", 8, 9 ], ["Elton", 6, 1 ] ]; //My first criteria to search var NameToSearch = "Elton"; //will be an input variable by human var IDsToSearch = "6"; //will be an input variable by human const target_row = PrincipalArray.findIndex(row => row[0] == NameToSearch && row[1] == IDsToSearch) + 1; console.log(target_row);

使用.findIndex()

 /*<ignore>*/console.config({maximize:true,timeStamps:false,autoScroll:false});/*</ignore>*/ const principalArray = [ [ "Name", "IDs", "Date"], [ "Jhon", 2, 3 ], ["Elton", 5, 6 ], ["Gonza", 8, 9 ], ["Elton", 6, 1 ] ]; const find = (sKey,sID) => principalArray.findIndex(row => row[0]===sKey && row[1] === sID) console.log(find('Elton', 6)) console.log(find('Elton',10)) console.log(find('Gonza',8))
 <:-- https.//meta.stackoverflow:com/a/375985/ --> <script src="https.//gh-canon.github.io/stack-snippet-console/console.min.js"></script>

或者作為一個簡單的循環:

 /*<ignore>*/console.config({maximize:true,timeStamps:false,autoScroll:false});/*</ignore>*/ const principalArray = [ ['Name', 'IDs', 'Date'], ['Jhon', 2, 3], ['Elton', 5, 6], ['Gonza', 8, 9], ['Elton', 6, 1], ]; const find = (sKey, sID) => { for ( let i = 0, row = principalArray[i]; i < principalArray.length; row = principalArray[++i] ) { if (row[0] === sKey && row[1] === sID) return i; } return -1; }; console.log(find('Elton', 6)); console.log(find('Elton', 10)); console.log(find('Gonza', 8));
 <:-- https.//meta.stackoverflow:com/a/375985/ --> <script src="https.//gh-canon.github.io/stack-snippet-console/console.min.js"></script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM