Sort javascript objects based on multiple properties

casillas

The dataSet contains arrays of javascript objects. Each object has the property color and weight. I am able sort the data based on the color. However I would like to know how to sort and combine data based on color and weight together and then push data to an

In other words, let say I have two arrays of javascript objects. I would like to find the pair of these objects (color, weight) in all dataSet then push.

Input:

dataSet[0].data= [
    {color:"yellow",weight:12}
   ,{color:"yellow", weight:12}
   ,{color:"yellow", weight:12}
   ,{color:"red",weight:13}
   ,{color:"red", weight:13}
];          
dataSet[1].data= [
    {color:"yellow",weight:12}
   ,{color:"yellow", weight:12}
   ,{color:"red",weight:13}
   ,{color:"red",weight:13}
   ,{color:"blue",weight:11}
   ,{color:"blue",weight:11}
];
colorMap=["red","green","blue","yellow","pink","black"];

Current output:

an[0]=[
    {color:"yellow",weight:12}
   ,{color:"yellow",weight:12}
   ,{color:"yellow",weight:12}
]
an[1]=[{color:"red",weight:13},{color:"red",weight:13}]
an[2]=[{color:"yellow",weight:12},{color:"yellow",weight:12}]
an[3]=[{color:"red",weight:13},{color:"red",weight:13}]
an[4]=[{color:"blue",weight:11},{color:"blue",weight:11}]

Desired output:

an[0]=[
   {color:"yellow",weight:12}
   ,{color:"yellow",weight:12}
   ,{color:"yellow",weight:12}
   ,{color:"yellow",weight:12}
   ,{color:"yellow",weight:12}
]
an[1]=[
    {color:"red",weight:13}
   ,{color:"red",weight:13}
   ,{color:"red",weight:13}
   ,{color:"red",weight:13}
]
an[2]=[{color:"blue",weight:11},{color:"blue",weight:11}]

Here is my current implementation

for (i = 0; i < dataSet.length; i++) {
  for (j = 0; j < self.colorMap.length; j++) {
      var d = dataSet[i].data.filter(function (x) { return x.color == self.colorMap[j] });
        if (d.length > 0) {
        an.push({ data: d, color: d[0].color, name:d[0].weight.toString()});
        }
    }
}
SpiderPig

If you use underscore you can use the groupBy function to help you here. Otherwise you can define groupBy yourself.

function groupBy(arr, f) {
  var result = {};
  arr.forEach(function(elem) {
    var fElem = f(elem),
        list = result[fElem] || [];
    list.push(elem);
    result[fElem] = list;
  });
  return result;
}

function objToArray(obj) {
  var result = [];
  for(k in obj) {
    result.push(obj[k]);
  }
  return result;
}

var dataSet = [];
dataSet[0]= [{color:"yellow",weight:12},{color:"yellow", weight:12},{color:"yellow", weight:12},{color:"red",weight:13},{color:"red", weight:13}];
dataSet[1]= [{color:"yellow",weight:12},{color:"yellow", weight:12},{color:"red",weight:13},{color:"red",weight:13},{color:"blue",weight:11},{color:"blue",weight:11}];


var allData = dataSet.reduce(function(a, b) { return a.concat(b) });
var grouped = objToArray(groupBy(allData, function(data) { return data.color + "#" + data.weight }));

console.log(grouped);

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Sort an array of objects by multiple properties

분류에서Dev

Sort an array of objects by multiple properties

분류에서Dev

JavaScript Objects - I'm having issues accessing an array of objects properties nested multiple levels & combining them together

분류에서Dev

Loop over a reference object multiple times to copy its properties to other objects in Javascript/Angularjs

분류에서Dev

Bash - sort filenames based on multiple fields

분류에서Dev

How to sort and display JSON objects based on one key value in php

분류에서Dev

How to sort arrays with multiple items - Javascript

분류에서Dev

Custom Object properties - multiple primitive variables or array of objects

분류에서Dev

WebGL/Javascript: Object transformations with multiple objects

분류에서Dev

How to sort data in an asp.net gridview by properties of sub-objects?

분류에서Dev

How do I sort an array filled with objects, based on two values of the object?

분류에서Dev

CustomComparator sort objects error

분류에서Dev

Change style.display on multiple objects through one button JAVASCRIPT

분류에서Dev

How do javascript engines set properties on objects that have prototypes with a setter for that property?

분류에서Dev

Compare and sort based on overlap

분류에서Dev

Sort List of Parent / Child Objects

분류에서Dev

qsort using to sort table of objects

분류에서Dev

Accessing properties of multiple SKShapeNodes

분류에서Dev

Sort on multiple attributes in an object

분류에서Dev

Implementing a iterator based shell sort

분류에서Dev

Group animation on multiple objects

분류에서Dev

Multiple objects, different behavior

분류에서Dev

JavaScript text nodes properties

분류에서Dev

Is JavaScript sort thorough?

분류에서Dev

Sort array of days in javascript

분류에서Dev

Modifying sort function in JavaScript

분류에서Dev

Javascript - sort issue

분류에서Dev

How to select multiple properties properly?

분류에서Dev

Ways to perform a sort for multiple data

Related 관련 기사

  1. 1

    Sort an array of objects by multiple properties

  2. 2

    Sort an array of objects by multiple properties

  3. 3

    JavaScript Objects - I'm having issues accessing an array of objects properties nested multiple levels & combining them together

  4. 4

    Loop over a reference object multiple times to copy its properties to other objects in Javascript/Angularjs

  5. 5

    Bash - sort filenames based on multiple fields

  6. 6

    How to sort and display JSON objects based on one key value in php

  7. 7

    How to sort arrays with multiple items - Javascript

  8. 8

    Custom Object properties - multiple primitive variables or array of objects

  9. 9

    WebGL/Javascript: Object transformations with multiple objects

  10. 10

    How to sort data in an asp.net gridview by properties of sub-objects?

  11. 11

    How do I sort an array filled with objects, based on two values of the object?

  12. 12

    CustomComparator sort objects error

  13. 13

    Change style.display on multiple objects through one button JAVASCRIPT

  14. 14

    How do javascript engines set properties on objects that have prototypes with a setter for that property?

  15. 15

    Compare and sort based on overlap

  16. 16

    Sort List of Parent / Child Objects

  17. 17

    qsort using to sort table of objects

  18. 18

    Accessing properties of multiple SKShapeNodes

  19. 19

    Sort on multiple attributes in an object

  20. 20

    Implementing a iterator based shell sort

  21. 21

    Group animation on multiple objects

  22. 22

    Multiple objects, different behavior

  23. 23

    JavaScript text nodes properties

  24. 24

    Is JavaScript sort thorough?

  25. 25

    Sort array of days in javascript

  26. 26

    Modifying sort function in JavaScript

  27. 27

    Javascript - sort issue

  28. 28

    How to select multiple properties properly?

  29. 29

    Ways to perform a sort for multiple data

뜨겁다태그

보관