使用保留关键字搜索数组键
Searching array key with reserved keyword
我创建了如下数组
["lists", { "getByTitle": "CardOperation" }, "items", { "filter": "Id eq 1" }, { "select": ["Title", "CustomerAge"] }];
现在一个键作为过滤器我想找到它知道它存在于数组中但是因为它是保留关键字我无法搜索它
我累了
array.filter //not working
array["filter"] // not working
在这种情况下我应该怎么做,我试图找出数组是否在任何索引处都有一个带有 filter 键的对象
这可能是您想要的:
这将遍历数组,如果存在具有键“filter”的对象,则 returns 为真,否则,returns 为假。
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
}
当然你可以过滤单词 filter 并且它不会成为保留的,除非你的结果有过滤方法
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
你必须搜索数组[3].filter
访问数组中的项目的最简单方法是使用它们的索引。
const array = ["arrays", { "getByTitle": "CardOperation" }, "items", { "filter": "Id eq 1" }, { "select": ["Title", "CustomerAge"] }];
console.log(array[3].filter);
/* Output: Id eq 1 */
希望你的问题得到解决
我创建了如下数组
["lists", { "getByTitle": "CardOperation" }, "items", { "filter": "Id eq 1" }, { "select": ["Title", "CustomerAge"] }];
现在一个键作为过滤器我想找到它知道它存在于数组中但是因为它是保留关键字我无法搜索它
我累了
array.filter //not working
array["filter"] // not working
在这种情况下我应该怎么做,我试图找出数组是否在任何索引处都有一个带有 filter 键的对象
这可能是您想要的:
这将遍历数组,如果存在具有键“filter”的对象,则 returns 为真,否则,returns 为假。
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
}
当然你可以过滤单词 filter 并且它不会成为保留的,除非你的结果有过滤方法
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
你必须搜索数组[3].filter
访问数组中的项目的最简单方法是使用它们的索引。
const array = ["arrays", { "getByTitle": "CardOperation" }, "items", { "filter": "Id eq 1" }, { "select": ["Title", "CustomerAge"] }];
console.log(array[3].filter);
/* Output: Id eq 1 */
希望你的问题得到解决