【问题标题】:Returning an object from an array based on matching a variable and a key根据匹配变量和键从数组中返回对象
【发布时间】:2022-02-04 20:32:08
【问题描述】:

我正在尝试从数组中提取与变量值匹配的对象。用户传递存储在名为 vars.author 的变量中的作者姓名。然后我想将该值与数组中的作者键匹配并返回包含该值的对象。

{
   "catalog": {
      "book": [
         {
            "author": "Ralls, Kim",
            "title": "Midnight Rain",
            "genre": "Fantasy",
            "price": "5.95",
            "publish_date": "2000-12-16",
            "description": "A former architect battles corporate zombies, \n      an evil sorceress, and her own childhood to become queen \n      of the world.",
            "_id": "bk102"
         },
         {
            "author": "Corets, Eva",
            "title": "Maeve Ascendant",
            "genre": "Fantasy",
            "price": "5.95",
            "publish_date": "2000-11-17",
            "description": "After the collapse of a nanotechnology \n      society in England, the young survivors lay the \n      foundation for a new society.",
            "_id": "bk103"
         },
         {
            "author": "Corets, Eva",
            "title": "Oberon's Legacy",
            "genre": "Fantasy",
            "price": "5.95",
            "publish_date": "2001-03-10",
            "description": "In post-apocalypse England, the mysterious \n      agent known only as Oberon helps to create a new life \n      for the inhabitants of London. Sequel to Maeve \n      Ascendant.",
            "_id": "bk104"
         },
         {
            "author": "Galos, Mike",
            "title": "Visual Studio 7: A Comprehensive Guide",
            "genre": "Computer",
            "price": "49.95",
            "publish_date": "2001-04-16",
            "description": "Microsoft Visual Studio 7 is explored in depth,\n      looking at how Visual Basic, Visual C++, C#, and ASP+ are \n      integrated into a comprehensive development \n      environment.",
            "_id": "bk112"
         }
      ]
   }
}

所以假设 vars.author 包含值“Corets, Eva”,我想返回以下内容:

{
            "author": "Corets, Eva",
            "title": "Maeve Ascendant",
            "genre": "Fantasy",
            "price": "5.95",
            "publish_date": "2000-11-17",
            "description": "After the collapse of a nanotechnology \n      society in England, the young survivors lay the \n      foundation for a new society.",
            "_id": "bk103"
         },
         {
            "author": "Corets, Eva",
            "title": "Oberon's Legacy",
            "genre": "Fantasy",
            "price": "5.95",
            "publish_date": "2001-03-10",
            "description": "In post-apocalypse England, the mysterious \n      agent known only as Oberon helps to create a new life \n      for the inhabitants of London. Sequel to Maeve \n      Ascendant.",
            "_id": "bk104"
         },

我尝试了以下变体,但没有任何效果。我看过这里,但没有完全匹配。如果有人可以向我推荐这里已经提出的问题,或者可以自己帮助我,我将不胜感激。感谢您的帮助。

payload.catalog.book
   filter ((item, index) -> item.author == vars.author) 
    map ((item, index) -> item.author)

更新: 感谢以下回复,找到了解决方案。谢谢大家。

payload filter ($.author == vars.author) map {
    author: $.author,
    title: $.title,
    genre: $.genre,
    price: $.price,
    publish_date: $.publish_date,
    description: $.description,
    id: $."_id"
}

【问题讨论】:

  • 您似乎错过了输出数组上的括号。它可以是一个对象,因为它缺少每个项目的键。一个对象不能有两个用逗号分隔的对象。
  • @ammanbesaw 。正如aled 提到的,您的输出将是对象数组,但是为什么您需要一个地图?只使用过滤器不能完成你的工作吗?

标签: json mule dataweave mulesoft


【解决方案1】:

正如 Aled 提到的,输出不是一个对象,而是一个数组。 为了满足您返回包含该值的对象的要求,您必须在流程中使用 foreach,然后发送每个对象。

而且正如 Karthik 所说,您不必在下面绘制地图仍然可以工作,

payload.catalog.book 过滤器 ((item, index) -> item.author == vars.author)

输出:

[ { “作者”:“Corets,伊娃”, "title": "Maeve Ascendant", “类型”:“奇幻”, “价格”:“5.95”, "publish_date": "2000-11-17", "description": "在英国纳米技术社会崩溃后,年轻的幸存者为新社会奠定了\n基础。", “_id”:“bk103” }, { “作者”:“Corets,伊娃”, "title": "Oberon 的遗产", “类型”:“奇幻”, “价格”:“5.95”, “发布日期”:“2001-03-10”, "description": "在后世界末日的英格兰,神秘的\n特工奥伯伦帮助伦敦的居民创造了新的生活\n。梅芙的续集\n Ascendant。", “_id”:“bk104” } ]

【讨论】:

  • 谢谢。这有帮助
【解决方案2】:

响应似乎是您的脚本没有最终地图。这将返回与作者姓名条件匹配的输入数组中的项组成的数组。

请注意,输出不是一个对象,而是一个数组,因为有多个可能的匹配项。例如,您可以使用 reduce 将其转换为对象,但您需要为每个值添加一个键。

【讨论】:

  • 谢谢。这对找出答案有很大帮助。我现在正试图弄清楚如何在未来为其他人更新它
猜你喜欢
  • 1970-01-01
  • 2018-10-17
  • 2019-05-28
  • 1970-01-01
  • 1970-01-01
  • 2021-04-09
  • 2019-09-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多