Javascript 如何通过它获取密钥';s映射属性值
如何从以下对象使用Javascript 如何通过它获取密钥';s映射属性值,javascript,arrays,Javascript,Arrays,如何从以下对象使用uniquevalue1获取key1?如果可能,如何通过“数组”中的othervalues获取密钥 { “键1”:{ anarray:['othervalues'], 值:“uniquevalue1” }, “键2”:{ 值:“uniquevalue2”, anarray:['othervalues'] } } 您可以。查找键值对: const [key] = Object.entries(input) .find(([, { value }]) => value
uniquevalue1
获取key1
?如果可能,如何通过“数组”中的othervalues
获取密钥
{
“键1”:{
anarray:['othervalues'],
值:“uniquevalue1”
},
“键2”:{
值:“uniquevalue2”,
anarray:['othervalues']
}
}
您可以。查找
键值对:
const [key] = Object.entries(input)
.find(([, { value }]) => value === "uniquevalue1");
下面是一个关于如何按值查找键的示例。
let arr={'key1':{anarray:['othervalues'],value:'uniquevalue1'},'key2':{value:'uniquevalue2',anarray:['othervalues']}
常量findKeyByValue=(val)=>{
用于(arr中的常量键){
if(arr[key].value.includes(val))
返回键;
}
};
log(findKeyByValue('uniquevalue1')代码>匹配字符串或数组中的值
函数getKeyByValue(_json,searchValue,searchInArray=false){
for(Object.entries(_json))的常量[key,val]{
常量{anarray,value}=val;
if(searchInArray&&anarray.includes(searchValue)){
返回键;
}else if(searchValue==值){
返回键;
}
}
}
风险值数据={
“键1”:{
anarray:['othervalues'],
值:“uniquevalue1”
},
“键2”:{
值:“uniquevalue2”,
anarray:['othervalues']
}
};
log(getKeyByValue(数据'uniquevalue1');
log(getKeyByValue(数据'othervalues',true))代码>