在多维数组中搜索键值对

时间:2016-11-18 21:13:49

标签: javascript arrays json

非常感谢您的阅读!

我有一个JSON对象,我需要搜索以查找是否存在值。我遇到的问题是查看一个数组,然后在Feature数组中,测试是否存在“In Ground Pool”。

谢谢!

targetListing是我的JSON对象

var targetFeature = "In Ground Pool";

function valuateFeature(targetFeature){
    for( var i = 0, len = targetListing.Features.length; i < len; i++ ) {
        if( targetListing.Features[i][0] === targetFeature ) {
            return true
        }else{
            return false
        }
    };

} ;

Address
:
Object
AgentEmail
:
"xxxxx"
AgentId
:
"xxxx"
AgentName
:
"Richard Getz"
AgentPhone
:
"xxxx"
AllListings
:
false
Bath
:
0
Bedroom
:
5
Brokerage
:
"xxxx"
BuildingStyle
:
"House"
BuildingStyleId
:
1
DaysPosted
:
0
Description
:
"If you've ever ..."
Features
:
Array[2]
0
:
Object
Description
:
"In Ground Pool"
__proto__
:
Object
1
:
Object
Description
:
"Attached Garage"
__proto__
:
Object
length
:
2
__proto__
:
Array[0]
HouseType
:
"Mediterranean"
HouseTypeId
:
3
Images
:
Array[0]

2 个答案:

答案 0 :(得分:0)

查看lodash.filter

var foundListings = _.filter(targetListing.Features, function() {
   return this[0] === targetFeature
}

var found = foundListings.length > 0;

这样的事情。有几个适当的lodash功能。 find或许

https://lodash.com/docs/4.17.2#filter

答案 1 :(得分:0)

我找到了它:)

我需要正确定位数组内部。

targetListing.Features[0]["Description"]