【问题标题】:filter JSON object based on specific value present inside nested object根据嵌套对象中存在的特定值过滤 JSON 对象
【发布时间】:2020-03-07 23:44:01
【问题描述】:

您好,我正在尝试过滤我的 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 中的 map 和 filter 来实现这一点,但不知道如何做到这一点并不好。 到目前为止,我正在尝试一些硬编码的方法。

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

但它会删除没有数量“0”的整个对象。

感谢您的帮助。

【问题讨论】:

  • .map(value => value) 写这个的目的是什么?这就像在做a=a
  • 我正在学习 es6 不知道如何正确地做。
  • 好的,在这种情况下你不需要使用filter 使用.map() 就足够了,但是你应该正确地转换映射的对象。

标签: javascript json ecmascript-6 filter reduce


【解决方案1】:

您只需要一个 Array#map() method 调用来映射所有response 对象并删除AAETAE 或目标keys 值具有amount

let keys = ["AAE", "TAE"];

let result = data.response.map(val => {
  keys.forEach(key => {
    if (val[key] && val[key].some(x => +x.amount <= 0)) {
      delete val[key];
    }
  });
  return val;
});

演示:

let keys = ["AAE", "TAE"];

let data = {
  "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"
      }]
    }
  ]
};

let result = data.response.map(val => {
  keys.forEach(key => {
    if (val[key] && val[key].some(x => +x.amount <= 0)) {
      delete val[key];
    }
  });
  return val;
});

console.log(result);

【讨论】:

  • 我有 20-30 个键作为“AAE”和“TAE”,如果没有每次都写入条件,我该如何实现。
  • @VirendersinghRathore 您可以将所有这些键放在一个数组中并对其进行迭代,请参阅我的编辑。
  • 真的很有帮助。
猜你喜欢
  • 2020-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-19
  • 2022-11-06
  • 2022-01-20
相关资源
最近更新 更多