2

suppose I have a JSON object, in the example users can give a rating to themselves

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

I know how to display this in a simple grid panel like below

enter image description here

But this is a bit confusing, is there an easy way to convert this into a matrix table grid similar to the format below?

enter image description here

Just looking for a pure JS / jquery way of doing it, many thanks for any input!

2
  • I think you are looking to pivotable Commented Mar 5, 2017 at 19:31
  • It's a matrix table I think I need, just unsure how to parse the json into one Commented Mar 5, 2017 at 19:44

1 Answer 1

2

You could use some hashes for the right row/col reference

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.