首页 > 解决方案 > 获取 JSON 数据数组中的最后一个字段

问题描述

我有下面的脚本,它根据我需要的路径拉回我需要的信息

let bodyData = pm.response.json()
let country = bodyData[2].Country
console.log(country)
pm.environment.set("country", country)

不必说 [2],无论作为邮递员请求的一部分,有多少对象被拉过,我都想撤回最后一个。

你能建议我怎么做吗?

标签: jsonpostman

解决方案


这应该对你有用,它使用的是应用程序内置的Lodash_.last方法:

let country = _.last(bodyData).Country

推荐阅读