Array.includes总是返回false

时间:2018-10-04 05:29:06

标签: javascript arrays

我在第0个数组元素中有一个对象,

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"}

我需要检查上述数组中是否有可用的密钥。我使用了includes()之类的Array.includes('Last Name')函数。但是它总是返回false。我有想念吗?我需要检查整个数组中的键,即使键或值也是如此。是否进行任何更改以检查Object中的密钥?

5 个答案:

答案 0 :(得分:2)

Array.prototype.includes()

  

includes()方法确定数组是否包含某个元素,并在适当时返回true或false。

Object.keys()

  

Object.keys()方法返回给定对象自己的属性名称的数组,其顺序与我们在普通循环中获得的顺序相同。

由于对象不是数组,因此可以使用Answer1 = 500500 Time taken = 4.558405389566795e-05 Answer2 = 500500 Time taken = 0.15728066685459452 Answer1 = 5000050000 Time taken = 0.005781152051264199 Answer2 = 5000050000 Time taken = 0.14532123447452705 Answer1 = 50000005000000 Time taken = 0.4903863230334036 Answer2 = 50000005000000 Time taken = 0.49744346392131533 Answer1 = 500000000500000000 Time taken = 50.825169837068 Answer2 = 500000000500000000 Time taken = 26.603663061636567 获取所有键的数组。您尝试匹配的密钥是Object.keys()而不是Last_Name__c

Last Name

答案 1 :(得分:1)

您要查找的方法是hasOwnProperty,这是JS Object类型的本机方法。像这样:

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

查看methodJS Object类型的文档。

答案 2 :(得分:0)

我犯的错误是,我从后端得到一个Map响应,并将其分配给前端的List属性,这就是为什么我像问题中那样以对象数组的形式获得响应。

现在,我将前端属性类型更改为Map,并进行了如下检查,

if(Object.keys(Array).includes(KEY) || Object.values(Array).includes(key))

它根据Mamun的答案起作用。

答案 3 :(得分:0)

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)

答案 4 :(得分:0)

我不确定这是否正确。

JSON.stringify(obj).includes(key)