繁体   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