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

Groupby Nested Array #86

Closed
xNok opened this issue Aug 3, 2017 · 1 comment
Closed

Groupby Nested Array #86

xNok opened this issue Aug 3, 2017 · 1 comment
Labels

Comments

@xNok
Copy link

xNok commented Aug 3, 2017

I`m trying to find a data mining library in JavaScript that would make it easy to manipulate JSON files. I have been recommended on stackoverflow to use this one. I have to say i like the Pandas like syntax to manipulate and summarize datasheets. However there is one use case I fail to solve with this library.

You have the initial JSON given a book you know the name of the author. In the Final JSON given an author you wanna know which book he wrote.

Intuitively I was looking to groupby author. But as a result the datas are grouped by group of authors.

Does this kind of transformation supported by Datalib? Is there a work around?

 [
     {
        "title": "",
        "author": [
          {
            "given": "",
            "family": "",
            "affiliation": []
          },
          {
            "given": "",
            "family": "",
            "affiliation": []
          }
         ]
      },{
        "title": "",
        "author": [
          {
            "given": "",
            "family": "",
            "affiliation": []
          },
          {
            "given": "",
            "family": "",
            "affiliation": []
          }
         ]
      }
    ]

Full example with input and desired output

@jheer jheer added the question label Aug 3, 2017
@jheer
Copy link
Member

jheer commented Aug 3, 2017

Datalib is focused on "flat" tabular data, so the groupby functionality won't be of great help in this use case with nested array data.

However, standard JavaScript array functions can meet your needs. Here's an example:

var byAuthor = books.reduce(function(authors, book) {
  book.author.forEach(function(author) {
    var name = author.family;
    var booksByAuthor = authors[name] || (authors[name] = []);
    booksByAuthor.push(book);
  });
  return authors;
}, {});

@jheer jheer closed this as completed Aug 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants