0

I have an array of objects that looks like that

[{ property1: 10 }, { property1: 13 }, { property1: 15 }, { property2: 2 }]

I want to write a function that would give me in an object all the indexes of comparison (only from left to right) of those properties if they are the same:

{ property1: [[0, 1], [0, 2], [1, 2]] }

For example, if my input array looks like that

[{ property1: 10 }, { property1: 13 }, { property2: 15 }, { property2: 2 }]

The result would be

{
    property1: [[0, 1]],
    property2: [[2, 3]]
}

If there are 4 same properties (the length of the input array will never be bigger than 4):

[{ property1: 10 }, { property1: 13 }, { property1: 15 }, { property1: 2 }]

The result should look like

{
    property1: [[0, 1], [0,2], [0,3], [1,2], [1,3], [2,3]]
}

Keys property1 are the same so I get their indexes (0, 1) and property2 keys are the same so I get their indexes (1, 2). Here there's only one combination possible as there are only 2 values that are the same for each but if there are more values that are the same I don't get how can I have the combinations of indexes. For the first example, I have 3 same keys (property1) so in case I need to know the combination of indexes between those 3, but only from left to right so (0,1), (0,2), and (1,2). I can see how to count the properties that are the same or return the indexes of the same properties but I don't see how can I get the possible combinations of indexes.

Le Rilleur
  • 205
  • 2
  • 15
  • Can you please clarify the logic here? With your second example it appears you simply want to collect the index positions of the objects that have that particular property(?) - but with your first example, I have no idea how you are getting to those numbers, `{ property1: [[0, 1], [0, 2], [2, 3]] }` ...? – CBroe Jul 25 '23 at 08:27
  • @CBroe I tried to add some details – Le Rilleur Jul 25 '23 at 08:31
  • Can you add other example with more properties? – kennarddh Jul 25 '23 at 08:35
  • 1
    The first example seems wrong. Why would there be [2, 3] in the expected result? Those two objects don't have the same property, while you write you want indices of objects with the *same* property. Did you make a mistake, and did you actually mean [1,2] instead of [2,3]? Please clarify. – trincot Jul 25 '23 at 08:37
  • What should happen if objects have more than one property? What if two such objects share one property, but not the other(s)? – trincot Jul 25 '23 at 08:39
  • @trincot Sorry yes indeed I made a mistake. The objects will only have one property – Le Rilleur Jul 25 '23 at 08:41
  • Ok but then you need to also correct the last paragraph of your text where again you mention (2,3). – trincot Jul 25 '23 at 08:42
  • Does this answer your question? [Pair every digit inside array with the others not including repeats or reversed pairs](https://stackoverflow.com/questions/48389959/pair-every-digit-inside-array-with-the-others-not-including-repeats-or-reversed) – jsejcksn Jul 25 '23 at 08:46

1 Answers1

1

You need to combine two algorithms:

  • Grouping objects by their (single) properties, giving for each the indices at which they occur in the input array.
  • Getting all pairs from an array of values. In your case these values will be a selection of indices from the input array.

Then combine the two by getting the pairs of the indices in each group.

Here is how it could be done:

function groupIndicesByProperty(arr) {
    const groups = {};
    arr.map(Object.keys).forEach(([key], i) => (groups[key] ??= []).push(i));
    return groups;
}

const pairsOf = values =>
    values.flatMap((a, i) => values.slice(i+1).map(b => [a, b]));
    
// Combine the two above:
const solve = (arr) => Object.fromEntries(
        Object.entries(groupIndicesByProperty(arr)).map(([key, indices]) =>
            [key, pairsOf(indices)]
        ).filter(([, {length}]) => length) // Exclude empty arrays
    );

// Three example runs:
let arr;
arr = [{ property1: 10 }, { property1: 13 }, { property1: 15 }, { property2: 2 }];
console.log(JSON.stringify(solve(arr)));
arr = [{ property1: 10 }, { property1: 13 }, { property2: 15 }, { property2: 2 }];
console.log(JSON.stringify(solve(arr)));
arr = [{ property1: 10 }, { property1: 13 }, { property1: 15 }, { property1: 2 }];
console.log(JSON.stringify(solve(arr)));
trincot
  • 317,000
  • 35
  • 244
  • 286