Home > Enterprise >  Searching array key with reserved keyword
Searching array key with reserved keyword

Time:12-20

I have created array as below

["lists", { "getByTitle": "CardOperation" }, "items", { "filter": "Id eq 1" }, { "select": ["Title", "CustomerAge"] }];

Now one key as filter I want find it to know it is exists in array but as it is reserved keyword i cannot search it

I tired

array.filter //not working

array["filter"] // not working

what should i do in this case, I am trying to find out if the array has an object with a key of filter at any index

CodePudding user response:

This is probably what you want:

This loops through the array and returns true if an object with key "filter" exists, or otherwise, returns 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
}

CodePudding user response:

Of course you can filter on the word filter and it does not become reserved unless your result has a filter method

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

CodePudding user response:

The easiest way to access the items in an array is to use their index.


const array = ["arrays", { "getByTitle": "CardOperation" }, "items", { "filter": "Id eq 1" }, { "select": ["Title", "CustomerAge"] }];

console.log(array[3].filter);

/* Output: Id eq 1 */


I hope your problem is solved

CodePudding user response:

You have to searcch array[3].filter

  • Related