在javascript中搜索嵌套对象数组

时间:2017-06-19 18:16:43

标签: javascript object

我有一个像下面这样的对象,并且我想为“SubscriptionScheme ='每日'检索”SubscriptionId“,如果存在或返回false。我有点挣扎结合过滤器和查找方法。提前谢谢。

final_df <- bind_rows(df2, append_df

所以预期的结果是 { [ { "SubscriptionId": "s1 ", "SubscriptionType": "User", "SubscriptionFieldList": { "SubscriptionFieldInfo": [ { "SubscriptionMethod": "Direct", "SubscriptionScheme": "Monthly" } ] } }, { "SubscriptionId": "s2 ", "SubscriptionType": "User", "SubscriptionFieldList": { "SubscriptionFieldInfo": [ { ":SubscriptionMethod": "Direct", "SubscriptionScheme": "daily" } ] } } ] }

1 个答案:

答案 0 :(得分:0)

您可以尝试以下内容:

var arr = [ 
       {
        "SubscriptionId": "s1 ",
        "SubscriptionType": "User",
        "SubscriptionFieldList": 
           {
             "SubscriptionFieldInfo": [
               {
                 "SubscriptionMethod": "Direct",
                 "SubscriptionScheme": "Monthly"
               }
             ]
          }
       },
       {
       "SubscriptionId": "s2 ",
       "SubscriptionType": "User",
       "SubscriptionFieldList": {
       "SubscriptionFieldInfo": [
          {
            ":SubscriptionMethod": "Direct",
            "SubscriptionFieldInfo": "daily"
          }
        ]
      }
     }
   ];
   
var obj = arr.filter(function(item){
    return item.SubscriptionFieldList.SubscriptionFieldInfo.some(x=>x.SubscriptionFieldInfo === "daily");
});

if(obj !== undefined && obj.length == 1){
    console.log(obj[0].SubscriptionId);
}