JMAC Docs

JMAC / docs / KeyExists

KeyExists()Method : Array.prototype

Sifts through an array of objects to check if a given property is defined


Values

function KeyExists(key: string, returns?: boolean, returnOnFail?: boolean): boolean|Array

Parameters

key
A string containing the name of the nested property key to search for.
Defaults to an empty String.
returns
If set to true, function will return the property values found.
Defaults to false.
returnOnFail
Test flag - if set to true, all non-matching indices will be logged in console.
Defaults to false.

Returns

boolean
Returns a boolean by default (true if key found).
Array
Returns array of found values when returns is enabled.

Common Errors

False
This method is designed to never throw an error - it returns false instead.

Example


const array1 = [
{"name": "connor", "age": 29, "sex": "M"},
{"name": "markus", "age": 32, "sex": "M"},
{"name": "kara", "age": 26, "sex": "F"}];

if (array1.KeyExists("name"))
{
    console.log("yep this exists")
}
else
{
    console.warn("names are missing");
}

console.log(newArray1);

// Should return:

yep this exists


Notes

  • returnOnFail will make the function log the array of non-matching indices irrespective of the state of returns.

Log

JMAC b | 21-05-25

Method introduced.