Home > Enterprise >  Is there a possibility to filter duplicate items by a field using Lodash?
Is there a possibility to filter duplicate items by a field using Lodash?

Time:09-04

Is there a possibility to filter duplicate items by a field using Lodash?

I have an array with several items, some of these items are duplicated, what differentiates is the validade field that is different.

Is it possible to filter and keep only one item?

Note: Null items must remain

[
  {
    "oa": 1,
    "id": 168,
    "lote": "2022-05-31-1672012800",
    "tipo_estoque": "MP",
    "data_entrada": "2022-07-11 10:25:25.0",
    "produto_id": 1,
    "item_id": 2594,
    "created_at": "2022-07-11 13:25:32.0",
    "updated_at": "2022-07-11 13:25:32.0",
    "validade": "2023-05-11 00:00:00.0",
    "siaId": "02.352"
  },
  {
    "oa": 1,
    "id": 228,
    "lote": "2022-05-31-1672012801",
    "tipo_estoque": "MP",
    "data_entrada": "2022-07-11 10:25:25.0",
    "produto_id": 1,
    "item_id": 2594,
    "created_at": "2022-07-11 13:25:32.0",
    "updated_at": "2022-07-11 13:25:32.0",
    "validade": "2023-06-11 00:00:00.0",
    "siaId": "02.352"
  },
  {
    "oa": 2,
    "id": null,
    "lote": null,
    "tipo_estoque": null,
    "data_entrada": null,
    "produto_id": null,
    "item_id": null,
    "created_at": null,
    "updated_at": null,
    "validade": null,
    "siaId": null
  },
  {
    "oa": 3,
    "id": 168,
    "lote": "2022-05-31-1672012800",
    "tipo_estoque": "MP",
    "data_entrada": "2022-07-11 10:25:25.0",
    "produto_id": 1,
    "item_id": 2594,
    "created_at": "2022-07-11 13:25:32.0",
    "updated_at": "2022-07-11 13:25:32.0",
    "validade": "2023-05-11 00:00:00.0",
    "siaId": "02.352"
  },
  {
    "oa": 3,
    "id": 228,
    "lote": "2022-05-31-1672012801",
    "tipo_estoque": "MP",
    "data_entrada": "2022-07-11 10:25:25.0",
    "produto_id": 1,
    "item_id": 2594,
    "created_at": "2022-07-11 13:25:32.0",
    "updated_at": "2022-07-11 13:25:32.0",
    "validade": "2023-06-11 00:00:00.0",
    "siaId": "02.352"
  },
  {
    "oa": 4,
    "id": null,
    "lote": null,
    "tipo_estoque": null,
    "data_entrada": null,
    "produto_id": null,
    "item_id": null,
    "created_at": null,
    "updated_at": null,
    "validade": null,
    "siaId": null
  },
  {
    "oa": 5,
    "id": null,
    "lote": null,
    "tipo_estoque": null,
    "data_entrada": null,
    "produto_id": null,
    "item_id": null,
    "created_at": null,
    "updated_at": null,
    "validade": null,
    "siaId": null
  }
]

CodePudding user response:

We group by the validade property (using Array.reduce method) only we don't care about it just push whatever is not duplicate into the result array.

var arr = [{
    "oa": 1,
    "id": 168,
    "lote": "2022-05-31-1672012800",
    "tipo_estoque": "MP",
    "data_entrada": "2022-07-11 10:25:25.0",
    "produto_id": 1,
    "item_id": 2594,
    "created_at": "2022-07-11 13:25:32.0",
    "updated_at": "2022-07-11 13:25:32.0",
    "validade": "2023-05-11 00:00:00.0",
    "siaId": "02.352"
  },
  {
    "oa": 1,
    "id": 228,
    "lote": "2022-05-31-1672012801",
    "tipo_estoque": "MP",
    "data_entrada": "2022-07-11 10:25:25.0",
    "produto_id": 1,
    "item_id": 2594,
    "created_at": "2022-07-11 13:25:32.0",
    "updated_at": "2022-07-11 13:25:32.0",
    "validade": "2023-06-11 00:00:00.0",
    "siaId": "02.352"
  },
  {
    "oa": 2,
    "id": null,
    "lote": null,
    "tipo_estoque": null,
    "data_entrada": null,
    "produto_id": null,
    "item_id": null,
    "created_at": null,
    "updated_at": null,
    "validade": null,
    "siaId": null
  },
  {
    "oa": 3,
    "id": 168,
    "lote": "2022-05-31-1672012800",
    "tipo_estoque": "MP",
    "data_entrada": "2022-07-11 10:25:25.0",
    "produto_id": 1,
    "item_id": 2594,
    "created_at": "2022-07-11 13:25:32.0",
    "updated_at": "2022-07-11 13:25:32.0",
    "validade": "2023-05-11 00:00:00.0",
    "siaId": "02.352"
  },
  {
    "oa": 3,
    "id": 228,
    "lote": "2022-05-31-1672012801",
    "tipo_estoque": "MP",
    "data_entrada": "2022-07-11 10:25:25.0",
    "produto_id": 1,
    "item_id": 2594,
    "created_at": "2022-07-11 13:25:32.0",
    "updated_at": "2022-07-11 13:25:32.0",
    "validade": "2023-06-11 00:00:00.0",
    "siaId": "02.352"
  },
  {
    "oa": 4,
    "id": null,
    "lote": null,
    "tipo_estoque": null,
    "data_entrada": null,
    "produto_id": null,
    "item_id": null,
    "created_at": null,
    "updated_at": null,
    "validade": null,
    "siaId": null
  },
  {
    "oa": 5,
    "id": null,
    "lote": null,
    "tipo_estoque": null,
    "data_entrada": null,
    "produto_id": null,
    "item_id": null,
    "created_at": null,
    "updated_at": null,
    "validade": null,
    "siaId": null
  }
]


var result = [];
arr.reduce(function(agg, item) {
  if (item.validade === null) {
    result.push(item)
  } else {

    if (!agg[item.validade]) {
      result.push(item)
      agg[item.validade] = true;
    } 
  }
  return agg;
}, {})

console.log(result)
.as-console-wrapper {max-height: 100% !important }

CodePudding user response:

Probably you are looking for uniqBy but it doesn't deal with the "null items must remain", here's an example:

const items = [/* your items, as described in the question */]
const uniques = _.uniqBy(items, (item) => item.validade)

If order doesn't matter then you could do it with a combo of partition and uniqBy:

const items = [/* your items, as described in the question */]
const [withNullDate, withNonNullDate] = _.partition(items, (item) => item.validade === null)
const uniquesWithNonNullDate = _.uniqBy(withNonNullDate, (item) => item.validade)
const uniquesAndNulls = [...uniquesWithNonNullDate, ...withNullDate]
  • Related