transpose(object)?

Ouverte
#48 3 commentaires 1 réaction 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

Personne n'a encore pris cette issue.

  • #158 par @Fil — fermée sans fusion

Évaluation

Difficulté
5/5
Temps estimé
Plus d'une semaine
Accessibilité débutants
20/100
Type d'issue
Fonctionnalité
Clarté
À clarifier
Activité
À l'abandon
Stack technique
javascript
Domaine
data

Piste de recherche

Read the existing transpose documentation in README.md and inspect the current d3.transpose implementation. The issue asks whether to overload it or introduce a separately named method, including behavior for both object-of-arrays and array-of-objects; settle the API and expected edge cases before implementation. Done means the chosen behavior is documented and covered by tests, but this issue gives no test file or decision on the API.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

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.

Langage dominant
JavaScript
Étoiles
465
Forks
193
Merge moyen
5 h 46 min
PR mergées (30 j)
1

Préparer son environnement

Ce projet ne fournit ni conteneur de développement, ni Dockerfile, ni guide de contribution : l'installation est à votre charge. Commencez par son README, et consultez notre guide de la première contribution pour les étapes générales.

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de d3/d3-array

Toutes les issues de d3/d3-array

Issues similaires

Plus d'issues JavaScript

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.