javascript - 根据嵌套对象中存在的特定值过滤 JSON 对象

标签 javascript json ecmascript-6 filter reduce

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

My response is below :

  {
    "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"
            }]
        }
    ]
}

I am trying to achieve :

{
"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”的整个对象。

感谢任何帮助。

最佳答案

您只需要一个 Array#map() method 调用以映射所有 response 对象并删除 AAETAE 或目标 keys 值有 数量 < 0:

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);

关于javascript - 根据嵌套对象中存在的特定值过滤 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58815732/

相关文章:

javascript - 每过一分钟就重新加载页面

javascript - 强制执行 show.bind

javascript - 为什么 instanceof 对 babel-node 下的 Error 子类的实例不起作用?

reactjs - React-native: super 表达式必须为 null 或函数,而不是未定义

javascript - 如何按值而不是键对 Map 对象数据进行排序?

javascript - Windows : Static files pending for up to two minutes? 上的 Node.js + Express.js

c# - 返回复杂类型以使用 AJAX 查看

php - 显示来自 MySQL 的特定 PHP 数组

json - 在Aeson中查看deriveJSON生成的代码

javascript - 使用 jQuery 按 ID 删除 DOM 元素