简体   繁体   English

从不同列中具有双重标准的二维数组中获取行号

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

I have a 2 dimensional array like this:我有一个像这样的二维数组:

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

(lets call PrincipalArray this data) (让我们调用 PrincipalArray 这个数据)

I have this array in a Google Sheet.我在 Google Sheet 中有这个数组。 I get this values to a 2 dimensional array in JavaScript in Google Apps Script.我在 Google Apps Script 中的 JavaScript 中将此值设为二维数组。

Then i want to filter something like: "Number of row who have the name Elton and the ID is 6" --> output is "row 3"然后我想过滤类似:“名称为 Elton 且 ID 为 6 的行数”-> output 为“第 3 行”

I need this because i need to edit this row when the criteria is true and i need to know the number of row.我需要这个,因为当条件为真时我需要编辑这一行并且我需要知道行数。 And if does not exist something with this criteria i need an answer like "-1.00" when indexof can not find something.如果不存在符合这个标准的东西,当 indexof 找不到东西时,我需要一个像“-1.00”这样的答案。

I will repeat this search many times, i mean, i need this automated.我会多次重复这个搜索,我的意思是,我需要这个自动化。

I was thinking in something similar like IndexOf but i can just IndexOf first column for Elton and does not exists and "indexOf with two criteria".我在考虑类似 IndexOf 的东西,但我可以只为 Elton 的第一列提供 IndexOf 并且不存在并且“具有两个条件的 indexOf”。

In pseudo-code i think this too:在伪代码中我也这么认为:

  1. Get first column with names (new 1 dimension array "NamesArray")获取具有名称的第一列(新的一维数组“NamesArray”)
  2. Get 2th column with IDs (new 1 dimension array "IDsArray")获取带有 ID 的第 2 列(新的一维数组“IDsArray”)
  3. Filter Array to know how much Eltons are in the array (length of my For Loop)过滤数组以了解数组中有多少埃尔顿(我的 For 循环的长度)
  4. Loop for with "how much eltons are in array" as length以“数组中有多少eltons”作为长度循环
  5. Make indexof in First Column for "elton" ( NamesArray.indexOf("Elton")在“elton”的第一列中创建 indexof( NamesArray.indexOf("Elton")
  6. Use the result of Indexof in Elton to verify if the 2th column have that ID in that row使用 Elton 中 Indexof 的结果来验证第二列是否在该行中具有该 ID
  7. iterate..迭代..

 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. } } }

Use findIndex()使用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);

Use .findIndex() :使用.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>

Or as a simple loop:或者作为一个简单的循环:

 /*<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