对数组对象进行分组,以便具有相似键的对象不会放入同一组中

时间:2018-04-17 16:31:52

标签: javascript arrays

我有以下数组名为' cars'。我想对数据进行分组,以便具有相似键的对象不会放入组(数组)。

var cars = [
{
    'make': 'audi',
    'model': 'r8',
    'year': '2012'
}, {
    'make': 'audi',
    'model': 'rs5',
    'year': '2013'
}, {
    'make': 'ford',
    'model': 'mustang',
    'year': '2012'
}, {
    'make': 'ford',
    'model': 'fusion',
    'year': '2015'
}, {
    'make': 'kia',
    'model': 'optima',
    'year': '2012'
},
];

如何对数据进行分组,以便将具有相同make的数组对象放入同一组中。结果将如下所示。

var cars = {
'class_1': [
    {   'make' : 'audi',
        'model': 'r8',
        'year': '2012'

    },{ 'make' : 'ford',
        'model': 'mustang',
        'year': '2012'
    },{
        'make': 'kia',
        'model': 'optima',
        'year': '2012'
      }
],


'class_2': [
    {
        'make' : 'audi',
        'model': 'rs5',
        'year': '2013'
    },{
        'make': 'ford',
        'model': 'fusion',
        'year': '2015'
    }
]
}

4 个答案:

答案 0 :(得分:1)

您可以使用Array.reduce()helper数组对项目进行分组。 helper数组包含每个组中的品牌。对于每个对象,您Array.findIndex()要查找不包含make的组。如果不存在,则为新组,并更新helper

const cars = [{"make":"audi","model":"r8","year":"2012"},{"make":"audi","model":"rs5","year":"2013"},{"make":"ford","model":"mustang","year":"2012"},{"make":"ford","model":"fusion","year":"2015"},{"make":"kia","model":"optima","year":"2012"}];

const helper = [];
const result = cars.reduce((r, o) => {
  let i = helper.findIndex((g) => !g[o.make]);
  if(i === -1) {
    i = helper.push({ [o.make]: true });
    r[`class_${i}`] = [o];
  } else {
    r[`class_${i + 1}`].push(o);
    helper[i][o.make] = true;
  }
    
  return r;
}, Object.create(null));

console.log(result);

答案 1 :(得分:0)

首先,您应该将汽车分组为一个物体(钥匙将是汽车的make,价值将是具有make的汽车数组。然后在这些分组数组中找到最大数组的长度(这将是类的数量)。然后通过从每个make数组中获取(如果存在)汽车来制作类:

function groupCars(cars) {
    // 1. group into an object
    var group = cars.reduce(function(acc, car) {                   // for each car in cars
        if(acc.hasOwnProperty(car.make)) {                         // if there is already an a sub array for this current car's make
            acc[car.make].push(car);                               // push it to that array
        } else {
            acc[car.make] = [car];                                 // if not, create a new sub array that initially contains this car
        }
        return acc;
    }, {});

    // 2. find the biggest array
    var makes = Object.keys(group);                                // get an array of all the makes (["audi", ...]) which are the keys of the object group
    var len = Math.max.apply(null, makes.map(function(make) { return group[make].length; })); // map each make into the length of its array of cars and choose the maximum of those length as len

    // 3. make the classes, picking up a car from each array (if it exists)
    var res = {};                                                  // our result object
    for(var i = 0; i < len; i++) {                                 // for...
        var cur = res["class_" + (i + 1)] = [];                    // make a new class array
        makes.forEach(function(make) {                             // for each make in makes
            if(i < group[make].length) {                           // if the current make's array is not empty yet
                cur.push(group[make][i]);                          // take the car at the current index and push it to the current class
            }
        });
    }
    return res;
}

可以使用ES6箭头功能缩短时间:

function groupCars(cars) {
    // 1. group into an object
    let group = cars.reduce((acc, car) => ((acc.hasOwnProperty(car.make)? acc[car.make].push(car): acc[car.make] = [car]), acc), {});

    // 2. find the biggest array
    let makes = Object.keys(group);
    let len = Math.max.apply(null, makes.map((make) => group[make].length));

    // 3. make the classes, picking up a car from each array (if it exists)
    let res = {};
    for(let i = 0; i < len; i++) {
        var cur = res["class_" + (i + 1)] = [];
        makes.forEach((make) => i < group[make].length && cur.push(group[make][i]);
    }
    return res;
}

示例:

function groupCars(cars) {
    let group = cars.reduce((acc, car) => ((acc.hasOwnProperty(car.make)? acc[car.make].push(car): acc[car.make] = [car]), acc), {});
    
    let makes = Object.keys(group);
    let len = Math.max.apply(null, makes.map((make) => group[make].length));

    let res = {};
    for(let i = 0; i < len; i++) {
        var cur = res["class_" + (i + 1)] = [];
        makes.forEach((make) => i < group[make].length && cur.push(group[make][i]);
    }
    return res;
}

var arr = [{"make":"audi","model":"r8","year":"2012"},{"make":"audi","model":"rs5","year":"2013"},{"make":"ford","model":"mustang","year":"2012"},{"make":"ford","model":"fusion","year":"2015"},{"make":"kia","model":"optima","year":"2012"}];


console.log(groupCars(arr));

答案 2 :(得分:0)

你可以这样做。

// helper function to add single car to single class if possible
function addCarToClass(car, cls) {
  // it the class already contains car with that key - fail
  for (let i = 0; i < cls.length; i++) {
    if (cls[i].make === car.make) return false;
  }
  // else push it to that class
  cls.push(car);
  return true;
}

// hepler function to find class to which the car should be placed
function findClassForCar(car, classes) {
  let placed = false;

  for (let i = 0; i < classes.length; i++) {
    if (addCarToClass(car, classes[i])) {
      placed = true;
      break;
    };
  }

  // if the car wasn't placed, start a new class and place it there
  if (!placed) {
    let cls = [];
    cls.push(car);
    classes.push(cls);
  }
}

// final function to split cars accordingly
function splitToClasses(cars) {
  const classes = [];

  for (let i = 0; i < cars.length; i++) {
    findClassForCar(cars[i], classes);
  }

  // format result according to your question
  const res = {};
  for (let i = 0; i < classes.length; i++) {
    res['class_' + i] = classes[i];
  }
  return res;
}

console.log(splitToClasses(cars));

答案 3 :(得分:0)

这可能效率不高,所以如果你在列表中处理数十万或更多的汽车,你可能会尝试找到其他东西。但代码很简单:

&#13;
&#13;
const cars = [{"make": "audi", "model": "r8", "year": "2012"}, {"make": "audi", "model": "rs5", "year": "2013"}, {"make": "ford", "model": "mustang", "year": "2012"}, {"make": "ford", "model": "fusion", "year": "2015"}, {"make": "kia", "model": "optima", "year": "2012"}]
 
const separateMakes = cars => cars.reduce((groupings, car) => {
  const groups = Object.values(groupings)
  let group = groups.find(group => group.every(test => test.make != car.make))
          || (groupings[`classes_${groups.length + 1}`] = [])
  group.push(car)
  return groupings
}, {})

console.log(separateMakes(cars))
&#13;
&#13;
&#13;