Check if any value in jsonPath is repeated

非 Y 不嫁゛ 提交于 2019-12-12 19:25:10

问题


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)

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?


回答1:


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() 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



来源:https://stackoverflow.com/questions/40954727/check-if-any-value-in-jsonpath-is-repeated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!