I need to calculate total numer of unread messages from Json array. Don´t have how to do it. Any help would be appreciated.
This could be done like this (for each topic):
recvId - readId
For example, if recvId
value is 7
and readId
value is 5
, the number of unread messages for this topic will be 2
This is my json array:
{"meta":{"id":"266124","topic":"me","sub":[
{"updatedAt":"1613057738448","acs":{"want":"J","given":"J"},"readId":4,"recvId":5,"public":"1","topic":"1","touchedAt":"1620054440432","seqId":4},
{"updatedAt":"1613058773611","acs":{"want":"J","given":"J"},"public":"1","topic":"2","touchedAt":"1613058773611"},
{"updatedAt":"1621412311168","acs":{"want":"J","given":"J"},"readId":52,"recvId":56,"public":"1","private":"2","topic":"3","touchedAt":"1621412311168","seqId":52,"delId":49},
{"updatedAt":"1616243250062","acs":{"want":"J","given":"J"},"readId":4,"recvId":4,"public":"1","private":"2","topic":"4","touchedAt":"1620054387252","seqId":4},
{"updatedAt":"1619091037093","acs":{"want":"N","given":"N"},"public":"1","topic":"5"},
{"updatedAt":"1612177368288","acs":{"want":"J","given":"J"},"readId":62,"recvId":70,"public":"1","private":"2","topic":"6","touchedAt":"1620081123060","seqId":62,"delId":20,"lastSeenTime":"1611669345000","lastSeenUserAgent":"C"},
{"updatedAt":"1621415453933","acs":{"want":"J","given":"J"},"readId":230,"recvId":230,"public":"1","topic":"7","touchedAt":"1621415453933","seqId":230,"delId":134,"lastSeenTime":"1621416294000","lastSeenUserAgent":"C"}
]}}
So, the total number that I need to get from this example is 13. As you can see, only in 3 topics readId values are different from recvId values, and the difference must be summed to get the number of unread messages. And some topics even don't contain these values.