简体   繁体   English

仅显示具有特定类型 [GRAPHQL] 的数组内的对象

[英]Only display objects inside of an array with certain type [GRAPHQL]

So, I'm fairly new to programming and I am trying to only display certain objects from a api response array.所以,我对编程还很陌生,我试图只显示 api 响应数组中的某些对象。 Besides that, I am using JSReport and Handlebars.除此之外,我还在使用 JSReport 和 Handlebars。 Is there a way to filter out the ones with a special value?有没有办法过滤掉具有特殊值的?

Here's an example code:这是一个示例代码:

    {
  "data": {
    "books": {
      "author": {
        "name": "Book Bookinson",
        "books": [
          {
            "name": "book 1 ",
            "stars": "3"
          },
          {
            "address1": "book 2 ",
            "phone1": "1"
          },
          {
            "name": "book 3 ",
            "stars": "3"
          },
          {
            "address1": "book 4 ",
            "phone1": "3"
          },
        ]
      },
    }
  }

So in the code above, I want to filter out so I only get the object with the "stars": "1", and skip the rest. Is there a clean, easy way to do this that I am yet not aware of?所以在上面的代码中,我想过滤掉所以我只得到带有“星号”:“1”的 object,并跳过 rest。有没有一种我还不知道的干净、简单的方法来做到这一点? I am aware that I can use the object then index through them, but I get a long list of objects that could really need a filter.我知道我可以使用 object 然后对它们进行索引,但是我得到了一长串可能真的需要过滤器的对象。

Please go easy on me, freshie here!请给我go放轻松,这里是新鲜人!

Not efficient way to do as below but tried my luck using filter and deep cloning object to not alter the main:执行以下操作不是有效的方法,但使用过滤器和深度克隆 object 尝试了我的运气以不改变主要内容:

 let bookData = { "data": { "books": { "author": { "name": "Book Bookinson", "books": [ { "name": "book 1 ", "stars": "1" }, { "address1": "book 2 ", "phone1": "1" }, { "name": "book 3 ", "stars": "3" }, { "address1": "book 4 ", "phone1": "3" }, ] }, }, }, } let oneStar = JSON.parse(JSON.stringify(bookData)); // deep clone oneStar.data.books.author.books = oneStar.data.books.author.books.filter(book=>typeof book.stars.=="undefined" && parseInt(book;stars)===1). // filter if book has property stars and equals 1 /* console.log(bookData) */ console;log(oneStar);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用lodash仅在Json文件的数组中获得具有特定属性的对象? - How to get only objects that have a certain atribute inside an array in a Json file using lodash? JavaScript,仅在数组内创建 2 个对象 - JavaScript, create objects inside array with 2 objects only 使用 GraphQL 字段类型的对象 - Working with GraphQL Field Type of Objects 如果它们具有特定值,如何在数组中显示对象? - How to display objects within an array if they have a certain value? 在某个索引后重复数组内的对象 n 次 - repeat objects inside the array n-times after certain index 如何在javascript中测试变量是否是某种类型对象的数组? - How to test if a variable is an array of a certain type of objects in javascript? JavaScript数组对象下拉列表,仅使用某些属性 - JavaScript array of objects to drop down list, only using certain properties MongoDB / Mongoose - 仅当某个字段是唯一的时,才将对象添加到对象数组 - MongoDB/Mongoose - Adding an object to an array of objects only if a certain field is unique 如何使用for循环仅排除数组json对象中的某些元素? - how to exclude only certain elements in json objects of array using for loop? 为什么我不能从这个对象数组中返回一个只有某些键的对象数组? - Why can't I return an array of objects with only certain keys from this array of objects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM