Reputation: 539
I have an Object in the Arrays 0' th element as follows,
Array = {CreatedById: "Created By ID",CreatedDate: "Created Date",Id:
"Record ID",IsDeleted: "Deleted",LastActivityDate: "Last Activity
Date",LastModifiedById: "Last Modified By ID",LastModifiedDate: "Last
Modified Date",Name: "Data Import Name",OwnerId: "Owner ID",SystemModstamp:
"System Modstamp",Last_Name__c: "Last Name",Mandate_Date_Signed__c: "Mandate
Date Signed",Mandate_End_Date__c: "Mandate End Date",Mandate__c: "Mandate"}
I'll need to check whether a key is available in the above array. I have used the includes()
function like Array.includes('Last Name')
. But it always return false
. Did I miss anything? I'll need to check the key in the whole array even in key or value. Any modification to check the key in the Object
?
Upvotes: 0
Views: 8728
Reputation: 340
I'm not sure if this is a correct way.
JSON.stringify(obj).includes(key)
Upvotes: 0
Reputation: 35
var mainArray = [
{
CreatedById: "Created By ID",
CreatedDate: "Created Date",
Id: "Record ID",
IsDeleted: "Deleted",
LastActivityDate: "Last Activity Date",
LastModifiedById: "Last Modified By ID",
LastModifiedDate: "Last Modified Date",
Name: "Data Import Name",
OwnerId: "Owner ID",
SystemModstamp: "System Modstamp",
Last_Name__c: "Last Name",
Mandate_Date_Signed__c: "Mandate Date Signed",
Mandate_End_Date__c: "Mandate End Date",
Mandate__c: "Mandate"
}]
function find(array,searchKey,searchValue, match=false ){
const findData = array.find(item=>!match ? item[searchKey] ===searchValue : item[searchKey].search(searchValue)>-1);
return findData ? true : false
}
console.log('search exact `Data Import Name` => ',find(mainArray , 'Name', 'Data Import Name'));
console.log('search exact `Data` => ',find(mainArray , 'Name', 'Data'));
console.log('search contains `Data` => ', find(mainArray , 'Name', 'Data', true));
console.log('search contains `majid` => ', find(mainArray , 'Name', 'majid', true))
var obj = {
CreatedById: "Created By ID",
CreatedDate: "Created Date",
Id: "Record ID",
IsDeleted: "Deleted",
LastActivityDate: "Last Activity Date",
LastModifiedById: "Last Modified By ID",
LastModifiedDate: "Last Modified Date",
Name: "Data Import Name",
OwnerId: "Owner ID",
SystemModstamp: "System Modstamp",
Last_Name__c: "Last Name",
Mandate_Date_Signed__c: "Mandate Date Signed",
Mandate_End_Date__c: "Mandate End Date",
Mandate__c: "Mandate"
}
console.log(Reflect.get(obj ,'Name') ? true : false)
console.log(Reflect.get(obj ,'other') ? true : false)
Upvotes: 0
Reputation: 539
The fault I made is, I got a Map response from the back end and assign it to a List attribute in the front end that's why I got the response as an Object Array like in the question.
Now I have changed the front-end attribute type as Map and checked as follows,
if(Object.keys(Array).includes(KEY) || Object.values(Array).includes(key))
and it works according to the Mamun's Answer.
Upvotes: 0
Reputation: 68933
The includes() method determines whether an array includes a certain element, returning true or false as appropriate.
The Object.keys() method returns an array of a given object's own property names, in the same order as we get with a normal loop.
Since the object is not array, you can use Object.keys()
to get an array of all the keys. The key you are trying to match is Last_Name__c
not Last Name
:
var obj = {
CreatedById: "Created By ID",CreatedDate: "Created Date",Id:
"Record ID",IsDeleted: "Deleted",LastActivityDate: "Last Activity Date",LastModifiedById: "Last Modified By ID",LastModifiedDate: "Last Modified Date",Name: "Data Import Name",OwnerId: "Owner ID",SystemModstamp: "System Modstamp",Last_Name__c: "Last Name",Mandate_Date_Signed__c: "Mandate Date Signed",Mandate_End_Date__c: "Mandate End Date",Mandate__c: "Mandate"};
var res = Object.keys(obj).includes('Last_Name__c');
console.log(res);
Upvotes: 2
Reputation: 967
The method you're looking for is hasOwnProperty
, which is a native method of the JS Object
type. Like so:
let array = {
CreatedById: "Created By ID",
CreatedDate: "Created Date",
Id: "Record ID",
IsDeleted: "Deleted",
LastActivityDate: "Last Activity Date",
LastModifiedById: "Last Modified By ID",
LastModifiedDate: "Last Modified Date",
Name: "Data Import Name",
OwnerId: "Owner ID",
SystemModstamp: "System Modstamp",
Last_Name__c: "Last Name",
Mandate_Date_Signed__c: "Mandate Date Signed",
Mandate_End_Date__c: "Mandate End Date",
Mandate__c: "Mandate"
}
console.log(array.hasOwnProperty('CreatedById') ? 'True' : 'False'); // Outputs True
console.log(array.hasOwnProperty('OtherProperty') ? 'True' : 'False'); // Outputs False
Check out the documentation for the method as well as the JS Object type.
Upvotes: 1