How to compare two arrays and remove duplicate obj

2019-08-18 16:52发布

问题:

I have two array of objects say,

var deSelectedRows = [
{ PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC01"},
{ PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC02"},
{ PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC03"},
{ PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC04"}
];

var selectedRows = [
{ PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC01"},
{ PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC02"},
{ PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC03"},
{ PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC04"}
];

And when I am trying to compare and iterate with two arrays with the below logic,

for  (var  i  of selectedRows) {
    for  (var  j  of deSelectedRows) {
      if  ( i.id  ===  j.id ) {
        selectedRows.splice(i,  1);
      }
    }
  }

Since the selected rows are being spliced, I am not able to iterate completely. Please help me with this.

回答1:

You could use a Set for the given id and filter selectedRows with the set.

var deSelectedRows = [{ PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC01"}, { PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC02"}, { PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC03"}, { PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC04"}],
    selectedRows = [{ PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC01"}, { PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC02"}, { PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC03"}, { PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC04"}],
    ids = new Set(deSelectedRows.map(({ id }) => id));

selectedRows = selectedRows.filter(({ id }) => !ids.has(id));

console.log(selectedRows);



回答2:

To avoid the mutation problem, you need to loop from the last element because the main problem are the visited elements.

var deSelectedRows = [{ PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC01"},{ PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC02"},{ PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC03"},{ PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC04"}];
var selectedRows = [{ PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC01"},{ PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC02"},{ PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC03"},{ PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC04"}];

var i = selectedRows.length; 
while (i--) {
  for (var j of deSelectedRows) {
    if (selectedRows[i] && selectedRows[i].id === j.id) {
      selectedRows.splice(i, 1);
    }
  }
}

console.log(selectedRows)



回答3:

You can use filter to achieve this functionality.

var deSelectedRows = [
{ PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC01"},
{ PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC02"},
{ PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC03"},
{ PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC04"}
];

var selectedRows = [
{ PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC01"},
{ PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC02"},
{ PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC03"},
{ PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC04"},
{ PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC05"}
];

selectedRows= selectedRows.filter(function(cv){
    return !deSelectedRows.find(function(e){
        return e.id == cv.id;
    });
});

console.log(selectedRows);