通过排序一个对象数组

时间:2016-06-25 19:47:38

标签: javascript arrays sorting object

给出以下对象数组:

var fruits = [
    { type: "banana", number: 20 },
    { type: "apple", number: 2 },
    { type: "pineapple", number: 40 },
    { type: "pineapple", number: 32 },
    { type: "banana", number: 80 },
    { type: "pineapple", number: 24 },
    { type: "apple", number: 64 },
    { type: "apple", number: 12 }
]

如何通过类型键对其进行排序,并为每种类型动态创建一个新的对象数组?获得这样的东西:

// new array of bananas:
[
  { type: "banana", number: 20 },
  { type: "banana", number: 80 }
]

// new array of apples:
[
  { type: "apple", number: 2 },
  { type: "apple", number: 64 },
  { type: "apple", number: 12 }
]

// new array of pineapples:
[
  { type: "pineapple", number: 40 },
  { type: "pineapple", number: 32 },
  { type: "pineapple", number: 24 }
]

5 个答案:

答案 0 :(得分:2)

这是我的解决方案,循环浏览项目,将其放入带有匹配键的列表中

    var fruits = [
    { type: "banana", number: 20 },
    { type: "apple", number: 2 },
    { type: "pineapple", number: 40 },
    { type: "pineapple", number: 32 },
    { type: "banana", number: 80 },
    { type: "pineapple", number: 24 },
    { type: "apple", number: 64 },
    { type: "apple", number: 12 }
];

var data = {};


fruits.forEach(function(fruit){
   if (data[fruit.type])  {
    data[fruit.type].push(fruit);
   } else {
      data[fruit.type] = [fruit];
   }
});

console.log(data);

答案 1 :(得分:1)

  

使用Array#filter

function方法会创建一个新数组,其中包含通过所提供的arr.filter(callback[, thisArg])实施的测试的所有元素。key

注意:您可以根据var fruits = [{ type: "banana", number: 20 }, { type: "apple", number: 2 }, { type: "pineapple", number: 40 }, { type: "pineapple", number: 32 }, { type: "banana", number: 80 }, { type: "pineapple", number: 24 }, { type: "apple", number: 64 }, { type: "apple", number: 12 }]; var filter = function(arr, key) { return arr.filter(function(item) { return item.type === key; }) } var apples = filter(fruits, 'apple'); var bananas = filter(fruits, 'banana'); var pineapples = filter(fruits, 'pineapple'); console.log(apples); console.log(bananas); console.log(pineapples);创建广义函数进行过滤。

import * as async from 'async';
const exec = require('child_process').exec;

async.series([
 exec('npm run dev'),
 exec('npm run test')
]); 

答案 2 :(得分:1)

您可以使用Array.prototype.reduce()Object.hasOwnProperty()

var res = fruits.reduce(function(obj, fruit) {
  if (!obj.hasOwnProperty(fruit.type)) {
    obj[fruit.type] = [fruit]
  } else {
    obj[fruit.type].push(fruit)
  }; 
  return obj
}, {});

console.log(res);

答案 3 :(得分:1)

我认为这是一个小组:

var fruits = [
    { type: "banana", number: 20 },
    { type: "apple", number: 2 },
    { type: "pineapple", number: 40 },
    { type: "pineapple", number: 32 },
    { type: "banana", number: 80 },
    { type: "pineapple", number: 24 },
    { type: "apple", number: 64 },
    { type: "apple", number: 12 }
]

var dict = {};
fruits.forEach(fruit => {
  dict[fruit.type] = (dict[fruit.type] || [])
  .concat([fruit]);
});

var groups = Object.keys(dict).map(k => dict[k]);

console.log(groups);

答案 4 :(得分:0)

使用ES6执行此操作的另一种奇特方式;

var fruits = [
    { type: "banana", number: 20 },
    { type: "apple", number: 2 },
    { type: "pineapple", number: 40 },
    { type: "pineapple", number: 32 },
    { type: "banana", number: 80 },
    { type: "pineapple", number: 24 },
    { type: "apple", number: 64 },
    { type: "apple", number: 12 }
],
   grouped = [...fruits.reduce((p,c) => p.set(c.type, p.has(c.type) ? p.get(c.type).concat(c)
                                                                    : [c]), new Map()).values()];
console.log(grouped);