最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

如何过滤JSON.parse结果

IT培训 admin 5浏览 0评论

如何过滤JSON.parse结果

我一直在尝试根据我的“ note”值过滤API调用的结果。我一直在Zapier上构建它,并且该调用有效,但是我似乎找不到找到使filter函数完成其工作的方法(因此,如果我将第19-23行替换为return results,则它将给我来自api调用)。我已经遍历了所有可以找到的堆栈文档,但是它们都以错误result.filter not foundbargle错误(Zapier中的一般错误)结尾。

const options = {
  url: `.json?`,
  method: 'GET',
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json'
  },
  params: {

  }
}

return z.request(options)
  .then((response) => {
    response.throwForStatus();
 
var results = z.JSON.parse(response.content);
var queryItem = "555-5555"
const filteredOrders = results.orders.filter(item => item.note === queryItem);
   return filteredOrders;
  
});
回答如下:

您正在尝试在对象中使用方法filter,但过滤器仅在数组中可用,因此您应尝试在orders数组中调用filter

let results = {
  "orders": [
     {
      "note": "555-5555",
      "subtotal_price": "1.00"
     },
     {
      "note": "555-6666",
      "subtotal_price": "2.00"
     } 
  ]
}

let queryItem = "555-5555";

let newArray = results.orders.filter(function (item) {
  return item.note == queryItem
})

console.log(newArray)

如何过滤JSON.parse结果

我一直在尝试根据我的“ note”值过滤API调用的结果。我一直在Zapier上构建它,并且该调用有效,但是我似乎找不到找到使filter函数完成其工作的方法(因此,如果我将第19-23行替换为return results,则它将给我来自api调用)。我已经遍历了所有可以找到的堆栈文档,但是它们都以错误result.filter not foundbargle错误(Zapier中的一般错误)结尾。

const options = {
  url: `.json?`,
  method: 'GET',
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json'
  },
  params: {

  }
}

return z.request(options)
  .then((response) => {
    response.throwForStatus();
 
var results = z.JSON.parse(response.content);
var queryItem = "555-5555"
const filteredOrders = results.orders.filter(item => item.note === queryItem);
   return filteredOrders;
  
});
回答如下:

您正在尝试在对象中使用方法filter,但过滤器仅在数组中可用,因此您应尝试在orders数组中调用filter

let results = {
  "orders": [
     {
      "note": "555-5555",
      "subtotal_price": "1.00"
     },
     {
      "note": "555-6666",
      "subtotal_price": "2.00"
     } 
  ]
}

let queryItem = "555-5555";

let newArray = results.orders.filter(function (item) {
  return item.note == queryItem
})

console.log(newArray)

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论