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

Andrux51

I'm trying to categorize an array of questions I've gotten from our application's API, and I've come up with a method that works, but it doesn't feel awesome. Can anyone tell me if there's a better way to do this?

Here's the code I have now. I'm not too worried about making it super optimized, but it shouldn't end up slow.

$scope.patron.categories = questions.map(function(question, i) {
    // check if question has a new CategoryId
    if(i === 0 || (i > 0 && question.CategoryId !== questions[i-1].CategoryId)) {
        // put questions with the same CategoryId together
        var catQuestions = questions.filter(function(q) {
            return q.CategoryId === question.CategoryId;
        });

        return {
            id: question.CategoryId,
            name: question.Category,
            collapsed: true,
            valid: false,
            questions: catQuestions
        };
    }
}).filter(function(category) {
    // because Array.prototype.map doesn't remove these by itself
    return category !== undefined;
});
Buzinas

Although it's primarily opinion-based, I prefer a simple loop.

For sure it's better for performance, and I think it's better for readability also.

var catIds = [], cats = [];

questions.forEach(function(q, i) {
  var idx = catIds.indexOf(q.CategoryId);
  if (idx === -1) {
    catIds.push(q.CategoryId);
    cats.push({
      id: q.CategoryId,
      name: q.Category,
      collapsed: true,
      valid: false,
      questions: [q]
    });
  }
  else {
    cats[idx].questions.push(q);
  }
});

$scope.patron.categories = cats;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

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

From Dev

efficient way to create JavaScript object from array of properties and array of matching property values

From Dev

Filter array of objects to get distinct values into new array

From Dev

getting distinct values from array of arrays in Javascript

From Dev

how to get a list of key values from array of objects - JavaScript

From Dev

Most efficient way to restructure complex array of objects in javascript?

From Dev

Efficient way of iterating over a array of objects in javascript/node.js

From Dev

What is the most efficient way of accessing a property from an array of objects in PowerShell?

From Dev

The most efficient way to get object with max value from list of objects

From Dev

How to get distinct values from nested lists of objects?

From Dev

JavaScript array of objects get duplicates by values

From Dev

Lodash get key values from array of objects

From Java

Extracting values from Array of nested Objects in JavaScript

From Dev

Javascript remove duplicate values from array of objects

From Dev

Efficient way to get float array from QByteArray with bigendian format

From Dev

Get all distinct values in a column as an array from a table

From Dev

How to get distinct values from an array in mongoDB and group them by ID

From Dev

Most efficient way to check if all given n objects are the same or distinct?

From Dev

How to distinct values from a 2D Array in JavaScript

From Dev

How to get all values from a JavaScript object (that contains an array of objects) using javascript

From Dev

Distinct values from DataFrame to Array

From Dev

Distinct values from DataFrame to Array

From Dev

More efficient way search for multiple objects in array

From Dev

Get distinct values from table

From Dev

How to get all values from an array of objects with nested arrays of objects?

From Dev

More elegant way of getting array with distinct values

From Dev

More elegant way of getting array with distinct values

From Dev

Efficient way to see if a set of values appear in an array?

From Dev

Efficient way to update values to array of hashes in ruby?

Related Related

  1. 1

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

  2. 2

    efficient way to create JavaScript object from array of properties and array of matching property values

  3. 3

    Filter array of objects to get distinct values into new array

  4. 4

    getting distinct values from array of arrays in Javascript

  5. 5

    how to get a list of key values from array of objects - JavaScript

  6. 6

    Most efficient way to restructure complex array of objects in javascript?

  7. 7

    Efficient way of iterating over a array of objects in javascript/node.js

  8. 8

    What is the most efficient way of accessing a property from an array of objects in PowerShell?

  9. 9

    The most efficient way to get object with max value from list of objects

  10. 10

    How to get distinct values from nested lists of objects?

  11. 11

    JavaScript array of objects get duplicates by values

  12. 12

    Lodash get key values from array of objects

  13. 13

    Extracting values from Array of nested Objects in JavaScript

  14. 14

    Javascript remove duplicate values from array of objects

  15. 15

    Efficient way to get float array from QByteArray with bigendian format

  16. 16

    Get all distinct values in a column as an array from a table

  17. 17

    How to get distinct values from an array in mongoDB and group them by ID

  18. 18

    Most efficient way to check if all given n objects are the same or distinct?

  19. 19

    How to distinct values from a 2D Array in JavaScript

  20. 20

    How to get all values from a JavaScript object (that contains an array of objects) using javascript

  21. 21

    Distinct values from DataFrame to Array

  22. 22

    Distinct values from DataFrame to Array

  23. 23

    More efficient way search for multiple objects in array

  24. 24

    Get distinct values from table

  25. 25

    How to get all values from an array of objects with nested arrays of objects?

  26. 26

    More elegant way of getting array with distinct values

  27. 27

    More elegant way of getting array with distinct values

  28. 28

    Efficient way to see if a set of values appear in an array?

  29. 29

    Efficient way to update values to array of hashes in ruby?

HotTag

Archive