首页 > 解决方案 > 如何从数组中提取特定值并将这些值分配给变量以在下一个邮递员请求中使用它

问题描述

在为邮递员编写 JavaScript 时,我有点陌生。我只想提取id动态的数字,每次向服务器发送请求时都会发生变化。所以我想提取这些变量并将其分配给我想在下一个邮递员删除请求中使用的变量。不知道它是如何做到的,任何帮助将不胜感激。

[{
  "id": 2286288,
  "tradeIt": true,
  "productId": 2066275,
  "accountId": 2805,

}, {
  "id": 2286290,
  "tradeIt": true,
  "productId": 2066275,
  "accountId": 2804,

}, {
  "id": 2286289,
  "tradeIt": true,
  "productId": 2066275,
  "accountId": 2806,

}, {
  "id": 2286291,
  "tradeIt": true,
  "productId": 2066275,
  "accountId": 2808,
  "assetId": 0,
}]

标签: javascriptarrayspostman

解决方案


如果这是一个数组,你应该用你想要的对象的索引来调用数组本身

//let's say 
arr = [[{ "id": 2286288, "tradeIt": true, "productId": 2066275, "accountId": 2805,

}, {
    "id": 2286290,
    "tradeIt": true,
    "productId": 2066275,
    "accountId": 2804,

}, {
    "id": 2286289,
    "tradeIt": true,
    "productId": 2066275,
    "accountId": 2806,    

}, {
    "id": 2286291,
    "tradeIt": true,
    "productId": 2066275,
    "accountId": 2808,
    "assetId": 0,
}
]

//to return for example id 2286291
let index =3;
arr[index].id;```

推荐阅读