我有一個像這樣的二維陣列:
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 的行數”--> 輸出為“第 3 行”
我需要這個,因為我需要在條件為真時編輯這一行,我需要知道行數。如果不存在符合此標準的內容,當 indexof 找不到某些內容時,我需要像“-1.00”這樣的答案。
我會多次重復這個搜索,我的意思是,我需要這個自動化。
我在想類似 IndexOf 的東西,但我只能為 Elton 第一列 IndexOf 并且不存在和“具有兩個標準的 indexOf”。
在偽代碼中,我也這么認為:
- 獲取帶有名稱的第一列(新的一維陣列“NamesArray”)
- 獲取帶有 ID 的第 2 列(新的一維陣列“IDsArray”)
- 過濾陣列以了解陣列中有多少 Elton(我的 For 回圈的長度)
- 回圈使用“陣列中有多少埃爾頓”作為長度
- 在“elton”的第一列中創建 indexof ( NamesArray.indexOf("Elton")
- 使用 Elton 中 Indexof 的結果來驗證第 2 列在該行中是否具有該 ID
- 迭代..
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.
}
}
}
uj5u.com熱心網友回復:
使用.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>
uj5u.com熱心網友回復:
使用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);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/371787.html
標籤:javascript 数组 谷歌应用程序脚本 谷歌表格
上一篇:如何在Javascript中創建一個唯一的json物件文字資料集?
下一篇:合并并獲取然后推送到一個陣列
