javascript – How to remove duplicate id from array of object by status?

Hello all I am trying to remove duplicate id from array by status but I am not getting it below is my array and code which I try.

[
    {
        "id": "1",
        "room_no": "Room No 101",
        "status": "confirm",
        "created_at": "2021-10-27 17:41:05",
        "bid": "70",
        "rid": "1",
        "guest_name": "test",
        "tariff": "2000",
        "apaid": "2000",
        "ckdate": "2022-06-17",
        "bstatus": "confirm"        
    },
    {
        "id": "1",
        "room_no": "Room No 101",
        "status": "confirm",
        "created_at": "2021-10-27 17:41:05",
        "bid": "72",
        "rid": "1",
        "guest_name": "sad",
        "tariff": "2222",
        "apaid": "2222",
        "ckdate": "2022-07-29",
        "bstatus": "reserve"        
    },
    {
        "id": "1",
        "room_no": "Room No 101",
        "status": "confirm",
        "created_at": "2021-10-27 17:41:05",
        "bid": "73",
        "rid": "1",
        "guest_name": "abid",
        "tariff": "2500",
        "apaid": "2500",
        "ckdate": "2022-06-15",
        "bstatus": "checkout"        
    },
    {
        "id": "2",
        "room_no": "Room No 102",
        "status": "checkout",
        "created_at": "2021-10-27 17:41:05",
        "bid": null,
        "rid": "2",
        "guest_name": null,
        "tariff": null,
        "apaid": null,
        "ckdate": null,
        "bstatus": null        
    },
    {
        "id": "3",
        "room_no": "Room No 103",
        "status": "checkout",
        "created_at": "2021-10-27 17:41:05",
        "bid": null,
        "rid": "3",
        "guest_name": null,
        "tariff": null,
        "apaid": null,
        "ckdate": null,
        "bstatus": null        
    },
    {
        "id": "4",
        "room_no": "Room No 104",
        "status": "checkout",
        "created_at": "2021-10-27 17:41:05",
        "bid": null,
        "rid": "4",
        "guest_name": null,
        "tariff": null,
        "apaid": null,
        "ckdate": null,
        "bstatus": null        
    },
    {
        "id": "5",
        "room_no": "Room No 105",
        "status": "checkout",
        "created_at": "2021-10-27 17:41:05",
        "bid": null,
        "rid": "5",
        "guest_name": null,
        "tariff": null,
        "apaid": null,
        "ckdate": null,
        "bstatus": null        
    },
    {
        "id": "6",
        "room_no": "Room No 106",
        "status": "checkout",
        "created_at": "2021-10-27 17:41:06",
        "bid": null,
        "rid": "6",
        "guest_name": null,
        "tariff": null,
        "apaid": null,
        "ckdate": null,
        "bstatus": null        
    },
    {
        "id": "7",
        "room_no": "Room No 201",
        "status": "checkout",
        "created_at": "2021-10-27 17:41:06",
        "bid": null,
        "rid": "7",
        "guest_name": null,
        "tariff": null,
        "apaid": null,
        "ckdate": null,
        "bstatus": null        
    },
    {
        "id": "8",
        "room_no": "Room No 202",
        "status": "checkout",
        "created_at": "2021-10-27 17:41:06",
        "bid": null,
        "rid": "8",
        "guest_name": null,
        "tariff": null,
        "apaid": null,
        "ckdate": null,
        "bstatus": null        
    },
    {
        "id": "9",
        "room_no": "Room No 203",
        "status": "checkout",
        "created_at": "2021-10-27 17:41:06",
        "bid": null,
        "rid": "9",
        "guest_name": null,
        "tariff": null,
        "apaid": null,
        "ckdate": null,
        "bstatus": null        
    },
    {
        "id": "10",
        "room_no": "Room No 204",
        "status": "checkout",
        "created_at": "2021-10-27 17:41:06",
        "bid": null,
        "rid": "10",
        "guest_name": null,
        "tariff": null,
        "apaid": null,
        "ckdate": null,
        "bstatus": null        
    },
    {
        "id": "11",
        "room_no": "Room No 205",
        "status": "checkout",
        "created_at": "2021-10-27 17:41:06",
        "bid": null,
        "rid": "11",
        "guest_name": null,
        "tariff": null,
        "apaid": null,
        "ckdate": null,
        "bstatus": null        
    },
    {
        "id": "12",
        "room_no": "Room No 206",
        "status": "checkout",
        "created_at": "2021-10-27 17:41:06",
        "bid": null,
        "rid": "12",
        "guest_name": null,
        "tariff": null,
        "apaid": null,
        "ckdate": null,
        "bstatus": null        
    }
]

I want to be remove duplicate id:1
but I want id:1 which has bstatus:"confirm"
if bstatus:"checkout" is present in object
and id only bstatus:"checkout" is present
than it will fetch bstatus:"checkout" only

expected result

[
    {
        "id": "1",
        "room_no": "Room No 101",
        "status": "confirm",
        "created_at": "2021-10-27 17:41:05",
        "bid": "70",
        "rid": "1",
        "guest_name": "test",
        "tariff": "2000",
        "apaid": "2000",
        "ckdate": "2022-06-17",
        "bstatus": "confirm"        
    },
    {
        "id": "2",
        "room_no": "Room No 102",
        "status": "checkout",
        "created_at": "2021-10-27 17:41:05",
        "bid": null,
        "rid": "2",
        "guest_name": null,
        "tariff": null,
        "apaid": null,
        "ckdate": null,
        "bstatus": null        
    },
    {
        "id": "3",
        "room_no": "Room No 103",
        "status": "checkout",
        "created_at": "2021-10-27 17:41:05",
        "bid": null,
        "rid": "3",
        "guest_name": null,
        "tariff": null,
        "apaid": null,
        "ckdate": null,
        "bstatus": null        
    },
    {
        "id": "4",
        "room_no": "Room No 104",
        "status": "checkout",
        "created_at": "2021-10-27 17:41:05",
        "bid": null,
        "rid": "4",
        "guest_name": null,
        "tariff": null,
        "apaid": null,
        "ckdate": null,
        "bstatus": null        
    },
    {
        "id": "5",
        "room_no": "Room No 105",
        "status": "checkout",
        "created_at": "2021-10-27 17:41:05",
        "bid": null,
        "rid": "5",
        "guest_name": null,
        "tariff": null,
        "apaid": null,
        "ckdate": null,
        "bstatus": null        
    },
    {
        "id": "6",
        "room_no": "Room No 106",
        "status": "checkout",
        "created_at": "2021-10-27 17:41:06",
        "bid": null,
        "rid": "6",
        "guest_name": null,
        "tariff": null,
        "apaid": null,
        "ckdate": null,
        "bstatus": null        
    },
    {
        "id": "7",
        "room_no": "Room No 201",
        "status": "checkout",
        "created_at": "2021-10-27 17:41:06",
        "bid": null,
        "rid": "7",
        "guest_name": null,
        "tariff": null,
        "apaid": null,
        "ckdate": null,
        "bstatus": null        
    },
    {
        "id": "8",
        "room_no": "Room No 202",
        "status": "checkout",
        "created_at": "2021-10-27 17:41:06",
        "bid": null,
        "rid": "8",
        "guest_name": null,
        "tariff": null,
        "apaid": null,
        "ckdate": null,
        "bstatus": null        
    },
    {
        "id": "9",
        "room_no": "Room No 203",
        "status": "checkout",
        "created_at": "2021-10-27 17:41:06",
        "bid": null,
        "rid": "9",
        "guest_name": null,
        "tariff": null,
        "apaid": null,
        "ckdate": null,
        "bstatus": null        
    },
    {
        "id": "10",
        "room_no": "Room No 204",
        "status": "checkout",
        "created_at": "2021-10-27 17:41:06",
        "bid": null,
        "rid": "10",
        "guest_name": null,
        "tariff": null,
        "apaid": null,
        "ckdate": null,
        "bstatus": null        
    },
    {
        "id": "11",
        "room_no": "Room No 205",
        "status": "checkout",
        "created_at": "2021-10-27 17:41:06",
        "bid": null,
        "rid": "11",
        "guest_name": null,
        "tariff": null,
        "apaid": null,
        "ckdate": null,
        "bstatus": null        
    },
    {
        "id": "12",
        "room_no": "Room No 206",
        "status": "checkout",
        "created_at": "2021-10-27 17:41:06",
        "bid": null,
        "rid": "12",
        "guest_name": null,
        "tariff": null,
        "apaid": null,
        "ckdate": null,
        "bstatus": null        
    }
]

My Code

for (var i = res.data.length - 1; i >= 0; i--) {
    if (index.indexOf(res.data[i].id) === -1) {
          if(res.data[i].bstatus !== 'reserve' && res.data[i].status !== 'cancel') {
            index.push(res.data[i].id);
            result.unshift(res.data[i]);
          }
    }
  }

any help would be appreciated thank you.

Leave a Comment