如果数组中存在对象,我想获取对象的值。 数据太大,为什么我想要快速的方法来获取它。 我的数组包含4000多个对象。 您建议我使用什么:简单循环,foreach,...?
我想在MY_GOAL_1对象的values数组中获取布尔值(如果存在)。
{
"extensionId": " ",
"version": "001",
"features": [
.....
.....
{
"logicalKey": "MY_GOAL_1",
"featureLabel": "Option Selected",
"typeCode": "BOOL",
"groupCode": "TOT",
"groupId": "*",
"isDisabled": false,
"isRequired": false,
"values": [
{
"value": true
}
]
},
{
"logicalKey": "MY_GOAL_2",
"featureLabel": "Option Selected",
"typeCode": "BOOL",
"groupCode": "TOT",
"groupId": "*",
"isDisabled": false,
"isRequired": false,
"values": [
{
"value": false
}
]
}
]
}
答案 0 :(得分:1)
您可以为此使用find。下面,我使用了您对象的子集。
var obj = {
"extensionId": " ",
"version": "001",
"features": [
{
"logicalKey": "MY_GOAL_1",
"featureLabel": "Option Selected",
"typeCode": "BOOL",
"groupCode": "TOT",
"groupId": "*",
"isDisabled": false,
"isRequired": false,
"values": [
{
"value": true
}
]
}
]
}
var foundObj = obj['features'].find(d => d.logicalKey == "MY_GOAL_1")
var result = foundObj && foundObj['values']
console.log(result || 'not found')