0

How can I delete one item in an array using ? I want to delete 1st data using patientserialno. I am pushing data like this:

BookingJobRequestEntity.push({
  PatientSerialNo: PatientSerialNo, 
  Department_SlNo: Department_SlNo, 
  IsHospitalBooking: IsHospitalBooking, 
  Pickupdate: Pickupdate, 
  Contact_SlNo: Contact_SlNo
});

I am having array like this view:

[{
  PatientSerialNo: 1, 
  Department_SlNo: 1, 
  IsHospitalBooking: false, 
  Pickupdate: "01/08/2017", 
  Contact_SlNo: "36"
},
{ 
  PatientSerialNo: 2, 
  Department_SlNo: 1, 
  IsHospitalBooking: false, 
  Pickupdate: "", 
  Contact_SlNo: "36"
},
{
  PatientSerialNo: 3, 
  Department_SlNo: 1, 
  IsHospitalBooking: false, 
  Pickupdate: "",  
  Contact_SlNo: "36"
}]
Dhasarathan T
  • 99
  • 1
  • 2
  • 14

2 Answers2

0

You can use Array.prototype.splice method, to remove an item, but you need to search for this item index in the array first.

You can also use Array.prototype.filter to filter your array data and remove the one with the specified PatientSerialNo:

BookingJobRequestEntity = BookingJobRequestEntity.filter(function(item){
      return item.PatientSerialNo != 1;
});

var BookingJobRequestEntity = [{
    PatientSerialNo: 1,
    Department_SlNo: 1,
    IsHospitalBooking: false,
    Pickupdate: "01/08/2017",
    Contact_SlNo: "36"
  },
  {
    PatientSerialNo: 2,
    Department_SlNo: 1,
    IsHospitalBooking: false,
    Pickupdate: "",
    Contact_SlNo: "36"
  },
  {
    PatientSerialNo: 3,
    Department_SlNo: 1,
    IsHospitalBooking: false,
    Pickupdate: "",
    Contact_SlNo: "36"
  }
]

var arr = BookingJobRequestEntity.filter(function(item){
      return item.PatientSerialNo != 1;
});
console.log(arr);
cнŝdk
  • 28,676
  • 7
  • 47
  • 67
-1

To find an index of an array you could use Array.findIdex.

The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise -1 is returned.

Parameters

callback: Function to execute on each value in the array, taking three arguments:

Arguments:

element: The current element being processed in the array.

index: The index of the current element being processed in the array.

array: The array findIndex was called upon.

callback: (Optional). Object to use as this when executing callback.

So, with the given index:...

You can use Array.splice where:

Parameters

start: Index at which to start changing the array (with origin 0). If greater than the length of the array, actual starting index will be set to the length of the array. If negative, will begin that many elements from the end of the array (with origin 1) and will be set to 0 if absolute value is greater than the length of the array.

deleteCount (Optional): An integer indicating the number of old array elements to remove. If deleteCount is 0, no elements are removed. In this case, you should specify at least one new element. If deleteCount is greater than the number of elements left in the array starting at start, then all of the elements through the end of the array will be deleted.

Return value: An array containing the deleted elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned.

e.g:

Array.splice(index, 1);

Where index is the position of the item that you need to remove.

Something like this:

(function() {
  var data = [{
      PatientSerialNo: 1,
      Department_SlNo: 1,
      IsHospitalBooking: false,
      Pickupdate: "01/08/2017",
      Contact_SlNo: "36"
    },
    {
      PatientSerialNo: 2,
      Department_SlNo: 1,
      IsHospitalBooking: false,
      Pickupdate: "",
      Contact_SlNo: "36"
    },
    {
      PatientSerialNo: 3,
      Department_SlNo: 1,
      IsHospitalBooking: false,
      Pickupdate: "",
      Contact_SlNo: "36"
    }
  ];

  function deleteObjectByPatientSerialNo(id) {
    var index = data.findIndex(function(x) {
      return x.PatientSerialNo === id;
    });
    if (index > -1) {
      data.splice(index, 1); // Only remove where the id matches with PatientSerialNo.
    }
    return data; // Returns the array.
  }
  data = deleteObjectByPatientSerialNo(1); // Set the PatientSerialNo to delete.
  console.log(data);
})();