I have json object and I try to remove duplicate entry based upon RoomId
and count duplicate data.
I try:
var listobj = [{
"RoomId": 8195,
"RoomName": "S-403 - Standard Room",
"Rate": 3000,
"Quantity": 1,
"PostinDate": "2023-03-31T18:15:00.000Z"
},
{
"RoomId": 8196,
"RoomName": "S-404 - Standard Room",
"Rate": 3000,
"Quantity": 1,
"PostinDate": "2023-03-31T18:15:00.000Z"
},
{
"RoomId": 8195,
"RoomName": "S-403 - Standard Room",
"Rate": 3000,
"Quantity": 1,
"PostinDate": "2023-04-01T18:15:00.000Z"
},
{
"RoomId": 8196,
"RoomName": "S-404 - Standard Room",
"Rate": 3000,
"Quantity": 1,
"PostinDate": "2023-04-01T18:15:00.000Z"
},
{
"RoomId": 8195,
"RoomName": "S-403 - Standard Room",
"Rate": 3000,
"Quantity": 1,
"PostinDate": "2023-04-02T18:15:00.000Z"
},
{
"RoomId": 8196,
"RoomName": "S-404 - Standard Room",
"Rate": 3000,
"Quantity": 1,
"PostinDate": "2023-04-02T18:15:00.000Z"
},
{
"RoomId": 8195,
"RoomName": "S-403 - Standard Room",
"Rate": 3000,
"Quantity": 1,
"PostinDate": "2023-04-03T18:15:00.000Z"
},
{
"RoomId": 8196,
"RoomName": "S-404 - Standard Room",
"Rate": 3000,
"Quantity": 1,
"PostinDate": "2023-04-03T18:15:00.000Z"
},
{
"RoomId": 8195,
"RoomName": "S-403 - Standard Room",
"Rate": 3000,
"Quantity": 1,
"PostinDate": "2023-04-04T18:15:00.000Z"
},
{
"RoomId": 8196,
"RoomName": "S-404 - Standard Room",
"Rate": 3000,
"Quantity": 1,
"PostinDate": "2023-04-04T18:15:00.000Z"
},
{
"RoomId": 8195,
"RoomName": "S-403 - Standard Room",
"Rate": 3000,
"Quantity": 1,
"PostinDate": "2023-04-05T18:15:00.000Z"
},
{
"RoomId": 8196,
"RoomName": "S-404 - Standard Room",
"Rate": 3000,
"Quantity": 1,
"PostinDate": "2023-04-05T18:15:00.000Z"
},
{
"RoomId": 8195,
"RoomName": "S-403 - Standard Room",
"Rate": 3000,
"Quantity": 1,
"PostinDate": "2023-04-06T18:15:00.000Z"
},
{
"RoomId": 8196,
"RoomName": "S-404 - Standard Room",
"Rate": 3000,
"Quantity": 1,
"PostinDate": "2023-04-06T18:15:00.000Z"
},
{
"RoomId": 8195,
"RoomName": "S-403 - Standard Room",
"Rate": 3000,
"Quantity": 1,
"PostinDate": "2023-04-07T18:15:00.000Z"
},
{
"RoomId": 8196,
"RoomName": "S-404 - Standard Room",
"Rate": 3000,
"Quantity": 1,
"PostinDate": "2023-04-07T18:15:00.000Z"
},
{
"RoomId": 8195,
"RoomName": "S-403 - Standard Room",
"Rate": 3000,
"Quantity": 1,
"PostinDate": "2023-04-08T18:15:00.000Z"
},
{
"RoomId": 8196,
"RoomName": "S-404 - Standard Room",
"Rate": 3000,
"Quantity": 1,
"PostinDate": "2023-04-08T18:15:00.000Z"
}
]
function groupByID(arr) {
var groupBy = {};
$.each(arr, function() {
groupBy[this.RoomId] = 1 + (groupBy[this.RoomId] || 0);
});
return groupBy;
}
function createArray(obj) {
var arr = [];
Object.keys(obj).forEach(function(key, val) {
arr.push({
RoomId: key,
RoomName: obj.RoomName,
Quantity: obj[key]
});
});
return arr;
}
var resObj = groupByID(listobj);
var resArray = createArray(resObj);
console.log(resArray);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Expected Out Put:
var out =[
{
"RoomId": 8195,
"RoomName": "S-403 - Standard Room",
"Rate": 3000,
"Quantity": 9,
"PostinDate": "2023-04-08T18:15:00.000Z"
},
{
"RoomId": 8196,
"RoomName": "S-404 - Standard Room",
"Rate": 3000,
"Quantity": 9,
"PostinDate": "2023-04-08T18:15:00.000Z"
}
]
How to catch other field like RoomName
,Rate
from json.
2
Answers
Finally I found a solution. Filter json based on
roomId
, count and update filtered json..