按特定顺序按属性值对数组对象进行排序

时间:2018-02-14 04:06:57

标签: javascript arrays sorting object

我有一个对象数组,我想使用每个对象的属性值与相应值的有序列表进行比较来排序。

假设我有这个字符串数组;食品集团:

[ 'protein',
  'dairy',
  'fruit',
  'vegetable' ]

我还有一系列物品和食品,每件都属于酒店group以前的食品集团之一:

[
  { group: 'vegetable', name: 'broccoli' },
  { group: 'protein', name: 'beef' },
  { group: 'fruit', name: 'apple' },
  { group: 'vegetable', name: 'peas' },
  { group: 'dairy', name: 'cheese' },
  { group: 'protein', name: 'tofu' },
  { group: 'vegetable', name: 'bell pepper' },
  { group: 'dairy', name: 'milk' },
  { group: 'fruit', name: 'grapes' },
  { group: 'protein', name: 'chicken' },
]

根据第一个数组中食物组的顺序,我如何使用对象group属性对食物进行排序,从而产生以下结果:

[
  { group: 'protein', name: 'beef' },
  { group: 'protein', name: 'tofu' },
  { group: 'protein', name: 'chicken' },
  { group: 'dairy', name: 'cheese' },
  { group: 'dairy', name: 'milk' },
  { group: 'fruit', name: 'apple' },
  { group: 'fruit', name: 'grapes' },
  { group: 'vegetable', name: 'broccoli' },
  { group: 'vegetable', name: 'peas' },
  { group: 'vegetable', name: 'bell pepper' },
]

虽然我在Javascript中这样做,但我确信在几种语言中这或多或少相同。

非常感谢任何帮助!

3 个答案:

答案 0 :(得分:2)

array.sort( (foodA, foodB) =>
  groups.indexOf( foodA.group ) - groups.indexOf( foodB.group )
)

我的示例是在ES6中,但可以在旧版本中轻松重写。以下是对sort函数 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

的引用

答案 1 :(得分:0)

您可以使用forEach& filter数组方法。迭代food groups&对于每个元素,过滤掉group中的匹配元素,&存储在新数组中



var order = ['protein',
  'dairy',
  'fruit',
  'vegetable'
]

var orgArry = [{
    group: 'vegetable',
    name: 'broccoli'
  },
  {
    group: 'protein',
    name: 'beef'
  },
  {
    group: 'fruit',
    name: 'apple'
  },
  {
    group: 'vegetable',
    name: 'peas'
  },
  {
    group: 'dairy',
    name: 'cheese'
  },
  {
    group: 'protein',
    name: 'tofu'
  },
  {
    group: 'vegetable',
    name: 'bell pepper'
  },
  {
    group: 'dairy',
    name: 'milk'
  },
  {
    group: 'fruit',
    name: 'grapes'
  },
  {
    group: 'protein',
    name: 'chicken'
  },
];
var newArray = [];
order.forEach(function(item) {
  return orgArry.filter(function(groupName) {
    if (groupName.group === item) {
      newArray.push(groupName)
    }
  })
});

console.log(newArray)




答案 2 :(得分:0)

您可以使用array#sort对数组进行排序,并查找group数组中存在的order索引,但排序不稳定。

var orders = [ 'protein', 'dairy', 'fruit', 'vegetable' ],
    data = [ { group: 'vegetable', name: 'broccoli' }, { group: 'protein', name: 'beef' }, { group: 'fruit', name: 'apple' }, { group: 'vegetable', name: 'peas' }, { group: 'dairy', name: 'cheese'}, { group: 'protein', name: 'tofu' }, { group: 'vegetable', name: 'bell pepper' }, { group: 'dairy', name: 'milk' }, { group: 'fruit', name: 'grapes' }, { group: 'protein', name: 'chicken' } ];
data.sort((a,b) => orders.indexOf(a.group) - orders.indexOf(b.group))
console.log(data);

要获得稳定的排序,您可以尝试以下实现。

var orders = [ 'protein', 'dairy', 'fruit', 'vegetable' ],
    data = [ { group: 'vegetable', name: 'broccoli' }, { group: 'protein', name: 'beef' }, { group: 'fruit', name: 'apple' }, { group: 'vegetable', name: 'peas' }, { group: 'dairy', name: 'cheese'}, { group: 'protein', name: 'tofu' }, { group: 'vegetable', name: 'bell pepper' }, { group: 'dairy', name: 'milk' }, { group: 'fruit', name: 'grapes' }, { group: 'protein', name: 'chicken' }],
    result = data.map(({group},i) => ({item:orders.indexOf(group), index: i}))
                .sort((a,b) => a.item - b.item || a.index - b.index)
                .map(({index}) => data[index]);
    
console.log(result);