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

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

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

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