Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transpose(object)? #48

Open
mbostock opened this issue Jan 26, 2017 · 3 comments
Open

transpose(object)? #48

mbostock opened this issue Jan 26, 2017 · 3 comments

Comments

@mbostock
Copy link
Member

mbostock commented Jan 26, 2017

We have transpose(matrix) which transposes the rows and columns of a matrix (an array of arrays). But you might want similar functionality to transpose an object whose values are arrays into an array whose elements are objects.

function transpose(object) {
  var m = 0;
  for (var k in object) m = Math.max(m, object[k].length);
  for (var i = -1, transpose = new Array(m); ++i < m;) {
    var o = transpose[i] = {};
    for (var k in object) {
      o[k] = object[k][i];
    }
  }
  return transpose;  
}

Where:

transpose({
  year: [2001, 2002],
  value: [1, 2]
})

Returns:

[
  {year: 2001, value: 1},
  {year: 2002, value: 2},
]

Should we try to overload d3.transpose to do both?

If so, how? The above implementation sort-of works for array-of-array input such as transpose([[0, 1, 2], [3, 4, 5]]), but returns an array of objects rather than an array of arrays. You could use Array.isArray to test whether the input is an Array and branch the behavior accordingly.

If not, what name should this new method have? transposeObject?

Also, if transpose(object) given an object whose values are arrays returns an array of objects, then transpose(array) given an array whose elements are objects should return an object whose values are arrays.

Maybe this is too magical.

@Fil
Copy link
Member

Fil commented Jun 11, 2020

I think it would be useful under the same name transpose, but with that name it absolutely must be involutive, ie transpose back [ {year: 2001, value: 1}, {year: 2002, value: 2}] to {year: [2001, 2002], value: [1, 2]}. Which makes auto-detection a bit harder (array of array, array of objects, object of arrays).
I've made a few tests here https://observablehq.com/d/e546523c2f1fbe2f

@mbostock
Copy link
Member Author

Perhaps it’s worth introducing two new methods: one for converting an array of objects to an object of arrays, and another for converting an object of arrays to an array of objects. Somewhat related, there’s also been a discussion on a pivot operation #142.

Fil added a commit that referenced this issue Jun 25, 2020
…, objects of arrays {[]} and objects of objects {{}}.

closes #48
@Fil
Copy link
Member

Fil commented Jun 25, 2020

Since there does not seem to be a conflict of definitions, I prefer to use transpose for the four cases: arrays of arrays [[]], arrays of objects [{}], objects of arrays {[]} and objects of objects {{}}.
#158

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants