根据嵌套对象内存在的特定值过滤JSON对象

时间:2019-11-12 09:33:45

标签: javascript json ecmascript-6 filter reduce

嗨,我正在尝试过滤JSON响应,以仅获取> = 0的值。

  

我的回答如下:

  {
    "response": [{
            "countryId": {
                "id": "10050020025",
                "idScheme": "externalId"
            },
            "processedDateTime": "2019-11-07 05:39:09",
            "AAE": [{
                "amount": "1000",
                "currencyCode": "USD"
            }],
            "TAE": [{
                "amount": "0",
                "currencyCode": "USD"
            }]
        },
        {
            "countryId": {
                "id": "10291520071",
                "idScheme": "InternalId"
            },
            "processedDateTime": "2019-02-03 08:21:22",
            "AAE": [{
                "amount": "0",
                "currencyCode": "USD"
            }],
            "TAE": [{
                "amount": "-2001",
                "currencyCode": "USD"
            }]
        }
    ]
}
  

我正在努力实现:

{
"response": [{
        "countryId": {
            "id": "10050020025",
            "idScheme": "externalId"
        },
        "processedDateTime": "2019-11-07 05:39:09",
        "AAE": [{
            "amount": "1000",
            "currencyCode": "USD"
        }]
    },
    {
        "countryId": {
            "id": "10291520071",
            "idScheme": "InternalId"
            },
            "processedDateTime": "2019-02-03 08:21:22",
        }
    ]
}

我正在尝试使用es6中的地图和过滤器来实现此目的,但是不好用,我不知道该怎么做。 到目前为止,我正在尝试一些硬编码方法。

const outputResponse = response
.map(value => value)
.filter(value => value.AAE[0].amount !== '0') 

但是它会删除不包含'0'的整个对象。

任何帮助,请感激。

1 个答案:

答案 0 :(得分:1)

您只需要一个 Array#map() method 调用即可映射所有onSuccessListener对象并删除responseAAE或定位的{{1 }}的值,其中TAE <0:

keys

演示:

amount