skip to Main Content

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


  1. Chosen as BEST ANSWER

    Finally I found a solution. Filter json based on roomId, count and update filtered json.

    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": 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": 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 countQuantity(obj) {
      var arr = [];
      Object.keys(obj).forEach(function(key, val) {
        arr.push({
          Quantity: obj[key]
    
        });
      });
      return arr;
    }
    var resObj = groupByID(listobj);
    var quantityArray = countQuantity(resObj);
    
    const result = listobj.filter(
      (thing, index, self) =>
      index ===
      self.findIndex((t) => t.RoomId === thing.RoomId)
    );
    
    result.forEach(function(i, x) {
      i.Quantity = quantityArray[x].Quantity;
    
    });
    console.log(result);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    .


  2. const roomList = [
      { roomId: 1, roomName: "Room 1" },
      { roomId: 2, roomName: "Room 2" },
      { roomId: 3, roomName: "Room 3" },
      { roomId: 1, roomName: "Room 1" },
      { roomId: 4, roomName: "Room 4" },
    ];
    const uniqueRooms = Array.from(new Set(roomList.map(room => room.roomName)))
      .map(roomName => {
        return roomList.find(room => room.roomName === roomName);
      });
    console.log(uniqueRooms)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search