2016-11-18 106 views
-2

非常感谢您的阅读!多维数组搜索键值对

我有一个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]

+0

如果您发布了JSON(使用jsonlint.com进行良好格式化)会更好。从你的问题中分析数据的结构是不可能的。 – Barmar

回答

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

+0

原生JavaScript数组已经有了广泛支持的过滤方法。 –

0

我发现它:)

我需要正确的阵列内的目标。

targetListing.Features[0]["Description"]