Delete (Remove) Duplicate object from JSON Array in JavaScript

Last Reply 27 days ago By dharmendr

Posted 29 days ago

i want to delete duplicate row from json result in javascript jquery.

i got json result in jqgrid_data = JSON.parse(result["jsoncoldata"]);

my jqgrid_data result is

0: {SR_No: 1, Batch_No: "", SerialNo: "REC20C011992", PerPackQty: 3.185, MaxOf_SerialNo: ""}
1: {SR_No: 1, Batch_No: "", SerialNo: "REC20C011994", PerPackQty: 3.863, MaxOf_SerialNo: ""}
2: {SR_No: 2, Batch_No: "", SerialNo: "REC20C011993", PerPackQty: 3.123, MaxOf_SerialNo: ""}
3: {SR_No: 3, Batch_No: "", SerialNo: "REC20C011992", PerPackQty: 3.185, MaxOf_SerialNo: ""}

but i want to delete duplicate SerialNo row from above var object in jquery javascript.

Posted 27 days ago

Hi varun.p,

Refer below code.

JavaScript

var array = [{ SR_No: 1, Batch_No: "", SerialNo: "REC20C011992", PerPackQty: 3.185, MaxOf_SerialNo: "" },
{ SR_No: 1, Batch_No: "", SerialNo: "REC20C011994", PerPackQty: 3.863, MaxOf_SerialNo: "" },
{ SR_No: 2, Batch_No: "", SerialNo: "REC20C011993", PerPackQty: 3.123, MaxOf_SerialNo: "" },
{ SR_No: 3, Batch_No: "", SerialNo: "REC20C011992", PerPackQty: 3.185, MaxOf_SerialNo: ""}];
// Declare a new array.
var newArray = [];
// Declare an empty object.
var uniqueSRNo = {};
// Loop in array elements.
for (var i in array) {
    // Extract distinct SerialNo.
    uniqueSRNo[array[i]['SerialNo']] = array[i];
}
// Loop through unique SerialNo and push object into array. 
for (var i in uniqueSRNo) {
    newArray.push(uniqueSRNo[i]);
}