E
E
Eugene Chefranov2020-05-04 21:48:20
JavaScript
Eugene Chefranov, 2020-05-04 21:48:20

How to filter an array of objects by occurrence?

There is such a table
5eb06188c38cc650306843.png
AND this table in the form of objects, one object = one row of the table:

var list = [
    { "n1": 18, "n2": 1, "n3": 5, "n4": 17 },
    { "n1": 1, "n2": 2, "n3": 11, "n4": 6 },
    { "n1": 1, "n2": 5, "n3": 11, "n4": 12 },
    { "n1": 6, "n2": 17, "n3": 12, "n4": 1 },
    { "n1": 4, "n2": 7, "n3": 8, "n4": 20 },
    { "n1": 2, "n2": 3, "n3": 8, "n4": 7 },
    { "n1": 2, "n2": 8, "n3": 7, "n4": 6 },
    { "n1": 14, "n2": 15, "n3": 11, "n4": 10 },
    { "n1": 11, "n2": 8, "n3": 16, "n4": 15 },
    { "n1": 20, "n2": 4, "n3": 8, "n4": 7 },
    { "n1": 11, "n2": 12, "n3": 6, "n4": 14 },
    { "n1": 1, "n2": 5, "n3": 12, "n4": 11 },
    { "n1": 17, "n2": 12, "n3": 5, "n4": 1 },
    { "n1": 8, "n2": 9, "n3": 13, "n4": 19 },
    { "n1": 8, "n2": 9, "n3": 19, "n4": 15 },
    { "n1": 10, "n2": 9, "n3": 15, "n4": 8 },
    { "n1": 11, "n2": 12, "n3": 17, "n4": 6 },
    { "n1": 8, "n2": 7, "n3": 20, "n4": 4 }
]

It is necessary to go through the array of objects and remove duplicate occurrences (4 digits) and leave only one.
For example, there is a string with values ​​1,5,11,12 and there are 1,5,12,11, you must leave one string (object) of these two, the order of numbers in the string may be different.

Visual example, end result:
5eb06663cad23599564514.png
And finally, just display the filtered array of objects.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2020-05-04
@Chefranov

I propose to make a JSON serialization of an array of sorted values ​​as a "fingerprint" of the string.
Then both will result in one hash:

{ "n1": 1, "n2": 5, "n3": 11, "n4": 12 } => "[1,5,11,12]"
{ "n1": 1, "n2": 5, "n3": 12, "n4": 11 } => "[1,5,11,12]"

The code:
const makeHash = (obj) => JSON.stringify(Object.values(obj).sort((a, b) => a - b));
const hashes = list.map(makeHash);
const result = list.filter((el, i) => i === hashes.indexOf(makeHash(el)))
result
[
  {
    "n1": 18,
    "n2": 1,
    "n3": 5,
    "n4": 17
  },
  {
    "n1": 1,
    "n2": 2,
    "n3": 11,
    "n4": 6
  },
  {
    "n1": 1,
    "n2": 5,
    "n3": 11,
    "n4": 12
  },
  {
    "n1": 6,
    "n2": 17,
    "n3": 12,
    "n4": 1
  },
  {
    "n1": 4,
    "n2": 7,
    "n3": 8,
    "n4": 20
  },
  {
    "n1": 2,
    "n2": 3,
    "n3": 8,
    "n4": 7
  },
  {
    "n1": 2,
    "n2": 8,
    "n3": 7,
    "n4": 6
  },
  {
    "n1": 14,
    "n2": 15,
    "n3": 11,
    "n4": 10
  },
  {
    "n1": 11,
    "n2": 8,
    "n3": 16,
    "n4": 15
  },
  {
    "n1": 11,
    "n2": 12,
    "n3": 6,
    "n4": 14
  },
  {
    "n1": 17,
    "n2": 12,
    "n3": 5,
    "n4": 1
  },
  {
    "n1": 8,
    "n2": 9,
    "n3": 13,
    "n4": 19
  },
  {
    "n1": 8,
    "n2": 9,
    "n3": 19,
    "n4": 15
  },
  {
    "n1": 10,
    "n2": 9,
    "n3": 15,
    "n4": 8
  },
  {
    "n1": 11,
    "n2": 12,
    "n3": 17,
    "n4": 6
  }
]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question