getting distinct values from array of arrays in Javascript

Arnab

My data is in the following format..

    var data= [['typeName', 'valueName'], ['type1', 'value1'],
['type1', 'value2'],['type2', 'value3'],['type2', 'value4']]

I wish to transform the above data to data as below..

    var resultdata=[{'typeName':'type1','valueName':['value1','value2']},
{'typeName':'type2','valueName':['value3','value4']}]

Basically I pick up distinct 'typeName' values and then group 'valueName' values by 'typeName' values.

I would preferably use only knockoutjs, lodash or underscorejs as my soln already uses them but I'm open to other solutions as well..

All help is sincerely appreciated

Thanks

Gruff Bunny

I think this solution using underscore should do the trick:

var result= _.chain(data)
    .rest()
    .groupBy( value => value[0])
    .map( (value,key) => ({ [data[0][0]]: key, [data[0][1]]: _.map(value, val => val[1])}))
    .value(); 

This solution uses rest to skip the first item in the data array (the type descriptors). The array is then grouped by the first value in the array (the type) and the mapping returns the grouping in the required form using es6 object initializer notation.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

populating array of arrays with distinct values

From Java

How to get distinct values from an array of objects in JavaScript?

From Dev

Javascript - Efficient way to get distinct values from an array of objects

From Dev

How to distinct values from a 2D Array in JavaScript

From Dev

More elegant way of getting array with distinct values

From Dev

More elegant way of getting array with distinct values

From Dev

Distinct values from DataFrame to Array

From Dev

Distinct values from DataFrame to Array

From Dev

Issue getting distinct values from column into array from mySQL via PHP

From Dev

Getting distinct values from table ignoring case

From Dev

Getting values from an array

From Dev

Making a quiz with Javascript. Getting array values from and object.

From Dev

Duplicate, Distinct and Unique values in Array JavaScript

From Dev

Duplicate, Distinct and Unique values in Array JavaScript

From Dev

JAVASCRIPT: Getting values inside an array

From Dev

selecting only distinct values from an array

From Dev

Extracting values from array of arrays

From Dev

Getting count of distinct values

From Dev

Getting first values from composite type arrays

From Dev

Getting specidic values from PHP arrays

From Dev

Getting first values from composite type arrays

From Dev

Adding values from arrays in JavaScript

From Dev

Getting and matching values from array

From Dev

Getting values from array in PHP

From Dev

Aggregating distinct values from JSONB arrays combined with SQL group by

From Dev

Getting values from Object in JavaScript

From Dev

What is a good way of getting distinct secondary index values from rethinkdb?

From Dev

What is a good way of getting distinct secondary index values from rethinkdb?

From Dev

Array of values from and array of arrays without loops

Related Related

  1. 1

    populating array of arrays with distinct values

  2. 2

    How to get distinct values from an array of objects in JavaScript?

  3. 3

    Javascript - Efficient way to get distinct values from an array of objects

  4. 4

    How to distinct values from a 2D Array in JavaScript

  5. 5

    More elegant way of getting array with distinct values

  6. 6

    More elegant way of getting array with distinct values

  7. 7

    Distinct values from DataFrame to Array

  8. 8

    Distinct values from DataFrame to Array

  9. 9

    Issue getting distinct values from column into array from mySQL via PHP

  10. 10

    Getting distinct values from table ignoring case

  11. 11

    Getting values from an array

  12. 12

    Making a quiz with Javascript. Getting array values from and object.

  13. 13

    Duplicate, Distinct and Unique values in Array JavaScript

  14. 14

    Duplicate, Distinct and Unique values in Array JavaScript

  15. 15

    JAVASCRIPT: Getting values inside an array

  16. 16

    selecting only distinct values from an array

  17. 17

    Extracting values from array of arrays

  18. 18

    Getting count of distinct values

  19. 19

    Getting first values from composite type arrays

  20. 20

    Getting specidic values from PHP arrays

  21. 21

    Getting first values from composite type arrays

  22. 22

    Adding values from arrays in JavaScript

  23. 23

    Getting and matching values from array

  24. 24

    Getting values from array in PHP

  25. 25

    Aggregating distinct values from JSONB arrays combined with SQL group by

  26. 26

    Getting values from Object in JavaScript

  27. 27

    What is a good way of getting distinct secondary index values from rethinkdb?

  28. 28

    What is a good way of getting distinct secondary index values from rethinkdb?

  29. 29

    Array of values from and array of arrays without loops

HotTag

Archive