我有一个 jsonPath 如下
{ "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
}
]}
并想检查是否有作者姓名重复?
我试过
$.book[?(@.author=='Nigel Rees')].find(1)
但是,它总是抛出一个没有发现任何异常的异常,我如何检查author='Nigel Rees'
事件即author='Nigel Rees'
有两本书吗?
如果作者姓名存在,取决于您打算做什么。
如果您只想要作者为的对象Nigel Reese
你可以使用filter https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter.
var booksByNigelReese = book.filter( function(book, index) {
return book.author === 'Nigel Reese'
})
.filter()
接受一个接受书籍和索引的函数,选择接受或拒绝这本书到一个新数组中,具体取决于函数的结果是否为true
or false
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)