问题描述
我有一个如下的jsonPath
I have a jsonPath as below
{ "book":
[
{ "category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{ "category": "fiction",
"author": "Nigel Rees",
"title": "Sword of Honour",
"price": 12.99
}
]}
还要检查是否重复了任何作者姓名?我尝试过
And Want to check if any author name have got repeated?I tried
$.book[?(@.author=='Nigel Rees')].find(1)
但是,它总是抛出一个什么都找不到的异常,我该如何检查author='Nigel Rees'
出现的地方,即author='Nigel Rees'
是否有两本书?
But, it always throws an exception that found nothing, how could I check that the author='Nigel Rees'
occurrences i.e author='Nigel Rees'
have a two books?
推荐答案
如果存在作者姓名,则取决于您计划执行的操作.如果只希望作者为Nigel Reese
的对象,则可以使用过滤器.
Depends what you are planning on doing if the authors names exists.If you only want the objects with author of Nigel Reese
you could use a filter.
var booksByNigelReese = book.filter( function(book, index) {
return book.author === 'Nigel Reese'
})
.filter()
带有一个接受书和索引的函数,根据该函数的结果是true
还是false
.filter()
takes a function that takes the book and index, chooes to accept or rejcet the book into a new array depending if the result of the function is true
or false
这篇关于检查jsonPath中是否有任何值重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!