首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于变量和键匹配的数组返回对象

基于变量和键匹配的数组返回对象
EN

Stack Overflow用户
提问于 2022-02-04 03:39:35
回答 2查看 231关注 0票数 0

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

代码语言:javascript
复制
{
   "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“值,我想返回以下内容:

代码语言:javascript
复制
{
            "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"
         },

我试过以下几种不同的方法,但都没有用。我看过这里,但没有完全吻合的地方。如果有人能告诉我一个已经在这里问过的问题,或者可以自己帮我,我会很感激的。谢谢你的帮助。

代码语言:javascript
复制
payload.catalog.book
   filter ((item, index) -> item.author == vars.author) 
    map ((item, index) -> item.author)

更新:通过下面的回复找到解决方案。谢谢大家。

代码语言:javascript
复制
payload filter ($.author == vars.author) map {
    author: $.author,
    title: $.title,
    genre: $.genre,
    price: $.price,
    publish_date: $.publish_date,
    description: $.description,
    id: $."_id"
}
EN

回答 2

Stack Overflow用户

发布于 2022-02-04 05:42:25

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

而且就像卡蒂克说的,你不必在下面的地图上工作,

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

产出:

{“作者”:"Corets,Eva",“标题”:"Maeve Ascendant",“类别”:"Fantasy","_id":"5.95","bk103“:"2000-11-17","description":”在英国纳米技术社会崩溃后,年轻的幸存者为一个新社会奠定了基础“,”_id“:”bk103“},{”作者“:"Corets,Eva",“标题”:“奥伯伦的遗产”、“流派”、“幻想”、“价格”:"5.95“、"publish_date":"2001-03-10”、“描述”:“在英国启示录后,只被称为奥伯伦的神秘\n特工帮助伦敦居民创造了新的生活。”“,”,"_id":"bk104“}

票数 2
EN

Stack Overflow用户

发布于 2022-02-04 03:50:49

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70981168

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档