mx_code
mx_code

Reputation: 2527

How to map the values of an Object to the number of occurences of its corresponding key in the whole array as an array?

I've been trying to figure out this for a couple of days. Nothing is working out.

Imagine that I've a list of objects like this:

let myArray = [
    { 0: 6, 5: 6 },
    { 0: 4, 11: 2, 13: 5 },
    { 0: 2 },
    { 0: 3, 13: 5 },
    { 0: 3, 13: 5 },
    { 0: 2 },
    { 0: 4, 3: 3, 13: 5 },
]

What I want to achieve:

{
    2: [],
    3: [],
    4: [],
    5: [],
    6: [],
}

So here is the explanation: I want to map the values of each objects in the array (which should be unique, no repeats) to the number of occurrences of its key in the whole array(myArray).

For example, if I take the first object in the array which is {0: 6, 5: 6}, I want to map its values (here 6) to the number of occurrences of those keys in the whole array(myArray)

{
6: [number of occurrences of the key 0 in the whole array (ie. myArray) with value 6, number of occurrences of the key 5 in the whole array (ie. myArray) with value 6]


... like so

}



Like this I want to generate the whole object: The result will be like this:

{
    2: [no. of occurence of key 0 with value 2 in myArray, no. of occurence of key 3 with value 2 in myArray, no. of occurence of key 5 with value 2 in myArray, no. of occurence of key 11 with value 2 in myArray, no. of occurence of key 13 with value 2 in myArray],
    3: [no. of occurence of key 0 with value 2 in myArray, no. of occurence of key 3 with value 2 in myArray, no. of occurence of key 5 with value 2 in myArray, no. of occurence of key 11 with value 2 in myArray, no. of occurence of key 13 with value 2 in myArray],
    4: [no. of occurence of key 0 with value 4 in myArray, no. of occurence of key 3 with value 4 in myArray, no. of occurence of key 5 with value 4 in myArray, no. of occurence of key 11 with value 4 in myArray, no. of occurence of key 13 with value 4 in myArray],
    5: [no. of occurence of key 0 with value 5 in myArray, no. of occurence of key 3 with value 5 in myArray, no. of occurence of key 5 with value 5 in myArray, no. of occurence of key 11 with value 5 in myArray, no. of occurence of key 13 with value 5 in myArray],
    6: [no. of occurence of key 0 with value 6 in myArray, no. of occurence of key 3 with value 6 in myArray, no. of occurence of key 5 with value 6 in myArray, no. of occurence of key 11 with value 6 in myArray, no. of occurence of key 13 with value 6 in myArray],
}

This is the result. SO in the above object the keys will be the values of each objects in the array myArray and the values will be the number of occurences of the keys of each objects in the array myArray.

It will be great if someone can help

Upvotes: 0

Views: 47

Answers (2)

sdotson
sdotson

Reputation: 820

Before you dive deeper down this road, perhaps ask yourself if there is an easier way to approach the problem you're trying to solve. The data structure you're proposing is admittedly a little confusing. In any case, I would like to help you. Below is how I would approach the problem:

const myArray = [
    { 0: 6, 5: 6 },
    { 0: 4, 11: 2, 13: 5 },
    { 0: 2 },
    { 0: 3, 13: 5 },
    { 0: 3, 13: 5 },
    { 0: 2 },
    { 0: 4, 3: 3, 13: 5 },
];

// key of result is val
// val is named tuple key 0, 3, 5, 11 occurence with key 

const process = (arr) => {
  return arr.reduce((acc, item) => {
    Object.entries(item).forEach(([key, val]) => {
      if (!acc[val]) {
        acc[val] = {};
      }
      if (!acc[val][key]) {
        acc[val][key] = 1;
      } else {
        acc[val][key] += 1;
      }
    });
    return acc;
  
  }, {});
};

console.log(process(myArray));

Upvotes: 1

Nenad Vracar
Nenad Vracar

Reputation: 122087

You could first count number of occurrences of each key-value pair in you data, then get sorted arrays of unique keys and values using Set, and then use reduce method to get the desired result.

let data = [{ 0: 6, 5: 6 },{ 0: 4, 11: 2, 13: 5 },{ 0: 2 },{ 0: 3, 13: 5 },{ 0: 3, 13: 5 },{ 0: 2 },{ 0: 4, 3: 3, 13: 5 },]

const count = data.reduce((r, o) => {
  Object.entries(o).forEach(([k, v]) => {
    let key = `${k}-${v}`;
    if (!r[key]) r[key] = 0
    r[key] += 1
  })

  return r
}, {})

const keys = [...new Set([].concat(...data.map(Object.keys)))].map(Number).sort((a, b) => a - b)
const values = [...new Set([].concat(...data.map(Object.values)))].map(Number).sort((a, b) => a - b)

const result = values.reduce((r, v) => {
  r[v] = keys.map(k => count[`${k}-${v}`] || 0)
  return r;
}, {})

console.log(result)

Upvotes: 1

Related Questions