简体   繁体   English

在JavaScript中对值数据数组进行排序

[英]Sorting value data arrays in JavaScript

I have the following array structure 我有以下数组结构

[
    [ "April", "January", "March", "February" ]
    [ "10", "20", "5", "2" ]
]

The months are not in order and the numbers represent the data for each month. 月份不整齐,数字代表每个月的数据。 For example January has 20 items, March has 5 items etc. 例如, January20项目, March5项目,等等。

Is there a way in JavaScript/jQuery I can keep the structure but ordered it as shown below? 在JavaScript / jQuery中,有没有一种方法可以保留结构但按如下所示对其进行排序?

[
    [ "January", "February", "March", "April" ]
    [ "20", "2", "5", "10" ]
]

If you must have an array form: 如果必须具有数组形式:

[{January: 20}, {Feb:2}, {March:5}, {April, 10}];

Seems to me it is a key to value pair. 在我看来,这是实现价值对的关键。 Use the Object! 使用对象!

As Rory suggests, it would be easier to keep the data together in a single object. 正如罗里(Rory)所建议的那样,将数据放在一个对象中会更容易。

[
  {
    month: "January",
    items: 20
  }, { // next object }
]

However, the task can be solved pretty easily anyways: 但是,无论如何该任务都可以轻松解决:

// first, define the correct order of months:
var sortedMonths = [ "January", "February", "March", "April" ]

// your unsorted months and items:
var months = [ "April", "January", "March", "February" ]
var items = [ "10", "20", "5", "2" ]
var sortedItems = []
sortedMonths.forEach(function(month) {
  // find index of the month in the unsorted array:
  var index = months.indexOf(month)
  // get item at the index and insert into sorted array:
  sortedItems.push(items[index])      
})

console.log(sortedItems) // ["20", "2", "5", "10"]

Here's one way, that puts them into an array of objects, sorts that array and then rebuilds the original structure: 这是一种将它们放入对象数组,对该数组进行排序然后重建原始结构的方法:

 var data = [ [ "April", "January", "March", "February" ], [ "10", "20", "5", "2" ] ]; // key for sorting months var monthSortIndex = {January: 1, February:2, March:3, April:4, May:5, June:6, July:7, August:8, September:9, October:10, November:11, December: 12}; function getMonthSort(m) { var val = monthSortIndex[m]; return val ? val: 13; } function sortMonthData(data) { // first put data into one array of objects var months = data[0]; var numbers = data[1]; months.map(function(value, index, array) { return {month: value, data: numbers[index]}; }).sort(function(a, b) { return getMonthSort(a.month) - getMonthSort(b.month); }).forEach(function(obj, index) { data[0][index] = obj.month; data[1][index] = obj.data; }); } sortMonthData(data); // show result in snippet document.write(JSON.stringify(data)); 

using underscore... 使用下划线...

var d = [ 
 [ "April", "January", "March", "February" ],
 [ "10", "20", "5", "2" ]
];

var months = {January:1, February:2, March:3, April:4}

var d = _.unzip(_.sortBy(_.zip(d[0],d[1]), function(a) { return months[a[0]] }))

This solution creates a temporary array that's based on the index of each month, so it's automatically sorted. 该解决方案基于每个月的索引创建一个临时数组,因此将自动对其进行排序。

It looks like this: 看起来像这样:

[{"month":"January","value":"20"},
 {"month":"February","value":"2"},
 {"month":"March","value":"5"},
 {"month":"April","value":"10"}
]

It's then a simple matter of looping through the array. 这是遍历数组的一个简单问题。

Snippet: 片段:

 var arr = [ ["April", "January", "March", "February"], ["10", "20", "5", "2"] ], months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], tmp= []; arr[0].forEach(function(mo, i) { tmp[months.indexOf(mo)]= { month: mo, value: arr[1][i] } }); tmp.forEach(function(key, i) { arr[0][i]= key.month; arr[1][i]= key.value; }); document.querySelector('pre').textContent= JSON.stringify(arr); 
 <pre></pre> 

Or with map: 或带地图:

var data = [
    [ "April", "January", "March", "February" ],
    [ "10", "20", "5", "2" ]
]

var order = [ "January", "February", "March", "April" ]

order.map(function(name){
  var index = data[0].indexOf(name);
  return  [data[0][index], data[1][index]]
}).forEach(function(element, index){
  data[0][index] = element[0];
  data[1][index] = element[1];
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM