[英]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.