javascript - Array.includes always return false - Stack Overflow

I have an Object in the Arrays 0' th element as follows,Array = {CreatedById: "Created By ID&

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?

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?

Share Improve this question edited Oct 4, 2018 at 6:57 Mamun 69k9 gold badges51 silver badges62 bronze badges asked Oct 4, 2018 at 5:29 HariprasathHariprasath 5391 gold badge10 silver badges21 bronze badges 13
  • I have an array as follows, Array = if you did that, then Array.includes will fail - you aren't overwriting the javascript Array object are you? – Jaromanda X Commented Oct 4, 2018 at 5:31
  • 8 That is not an array. It is an object. – Akrion Commented Oct 4, 2018 at 5:31
  • @JaromandaX even if he did, it would still fail. – Patrick Roberts Commented Oct 4, 2018 at 5:31
  • Huh @PatrickRoberts? I said Array.includes will fail ... not return false ... but will fail - oh, you mean "even if he didn't" [overwrite Array] - yeah, I know that, because it's not an Array – Jaromanda X Commented Oct 4, 2018 at 5:32
  • 2 @JaromandaX Actually overwriting the global Array wouldn't make instance.includes(…) fail, but of course this snippet is not creating an array instance. – Bergi Commented Oct 4, 2018 at 5:34
 |  Show 8 more ments

5 Answers 5

Reset to default 2

Array.prototype.includes()

The includes() method determines whether an array includes a certain element, returning true or false as appropriate.

Object.keys()

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);

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.

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.

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)

I'm not sure if this is a correct way.

JSON.stringify(obj).includes(key)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745377998a4625090.html

相关推荐

  • javascript - Array.includes always return false - Stack Overflow

    I have an Object in the Arrays 0' th element as follows,Array = {CreatedById: "Created By ID&

    6小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信