如何将一个对象的值映射到作为数组的整个数组中其对应键的出现次数?

我已经尝试了几天了。什么都没有解决。

想象一下,我有一个这样的对象列表:

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 },
]

我要实现的目标:

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

所以这是解释: 我想将数组中每个对象的值(应该是唯一的,不重复)映射到其在整个array(myArray)中的键的出现次数。

例如,如果我取数组中的第一个对象为{0:6,5:6},则要将其值(此处为6)映射到整个数组中这些键的出现次数(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

}



这样,我想生成整个对象: 结果将是这样的:

{
    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],
}

这就是结果。 因此,在上述对象中,键将是数组myArray中每个对象的值,而值将是数组myArray中每个对象中键的出现次数。

如果有人可以帮忙,那就太好了

评论
  • zearum
    zearum 回复

    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)
  • 妮听咱话
    妮听咱话 回复

    在深入研究这条路之前,也许要问自己,是否有更简单的方法来解决您要解决的问题。您所提出的数据结构确实有些混乱。无论如何,我想为您提供帮助。以下是我如何解决该问题的方法:

    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));