gpt4 book ai didi

javascript - json转矩阵表? Javascript/HTML

转载 作者:行者123 更新时间:2023-11-29 21:08:59 25 4
gpt4 key购买 nike

假设我有一个 JSON 对象,在示例中用户可以给自己打分

[
{
"to": "jeff",
"rating": 50,
"from": "jeff"
},
{
"to": "bob",
"rating": 50,
"from": "jeff"
},
{
"to": "jeff",
"rating": 75,
"from": "bob"
},
{
"to": "bob",
"rating": 75,
"from": "bob"
}
]

我知道如何在如下所示的简单网格面板中显示它

enter image description here

但这有点令人困惑,有没有一种简单的方法可以将其转换为类似于下面格式的矩阵表格网格?

enter image description here

只是在寻找一种纯 JS/jquery 的方式,非常感谢您的任何输入!

最佳答案

您可以对正确的行/列引用使用一些散列

var data = [{ "to": "jeff", "rating": 50, "from": "jeff" }, { "to": "bob", "rating": 50, "from": "jeff" }, { "to": "jeff", "rating": 75, "from": "bob" }, { "to": "bob", "rating": 75, "from": "bob" }],
result = [['to/from']];

data.forEach(function (row, col) {
return function (a) {
if (!(a.to in row)) {
row[a.to] = result.push([a.to]) - 1;
}
if (!(a.from in col)) {
col[a.from] = result[0].push(a.from) - 1;
}
result[row[a.to]][col[a.from]] = a.rating;
};
}(Object.create(null), (Object.create(null))));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - json转矩阵表? Javascript/HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42612921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com