我創建了如下陣列
["lists", { "getByTitle": "CardOperation" }, "items", { "filter": "Id eq 1" }, { "select": ["Title", "CustomerAge"] }];
現在一個鍵作為過濾器我想找到它以知道它存在于陣列中但由于它是保留關鍵字我無法搜索它
我累了
array.filter //not working
array["filter"] // not working
在這種情況下我應該怎么做,我試圖找出陣列是否有一個物件,在任何索引處都有一個過濾器的鍵
uj5u.com熱心網友回復:
這可能是你想要的:
如果存在鍵為“filter”的物件,則回圈遍歷陣列并回傳 true,否則回傳 false。
const myArray = ["lists", { "getByTitle": "CardOperation" }, "items", { "filter": "Id eq 1" }, { "select": ["Title", "CustomerAge"] }]
const hasObject = myArray.find(obj => typeof obj === 'object' && obj !== null && obj["filter"]) ? true : false;
console.log(hasObject);
const objectIndex = myArray.findIndex(obj => typeof obj === 'object' && obj !== null && obj["filter"]);
if(objectIndex == -1) {
// there is no match in the array
}
uj5u.com熱心網友回復:
當然,您可以過濾詞過濾器,除非您的結果具有過濾器方法,否則它不會被保留
const list = ["lists", { "getByTitle": "CardOperation" }, "items", { "filter": "Id eq 1" }, { "select": ["Title", "CustomerAge"] }];
const obj = list
.filter(item => typeof item === "object" &&
Object.keys(item).includes("filter")); // loking for the key name "filter"
console.log(obj[0].filter); // using the key named filter
uj5u.com熱心網友回復:
訪問陣列中專案的最簡單方法是使用它們的索引。
const array = ["arrays", { "getByTitle": "CardOperation" }, "items", { "filter": "Id eq 1" }, { "select": ["Title", "CustomerAge"] }];
console.log(array[3].filter);
/* Output: Id eq 1 */
我希望你的問題得到解決
uj5u.com熱心網友回復:
你必須搜索 array[3].filter
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/387207.html
標籤:javascript 数组 多维数组
上一篇:如何在類陣列中檢查用戶輸入的字串
下一篇:源有5個元素,但目標只允許0
