如何将对象数组转换为嵌套数组

时间:2017-10-21 17:27:29

标签: javascript arrays lodash

示例数据:

var people = [
  { name: 'John', age: 30, gender: 'male' },
  { name: 'Kelly', age: 31, gender: 'female' },
  { name: 'Stephanie', age: 30, gender: 'female' },
  { name: 'Dan',  age: 28, gender: 'male' },
  { name: 'Crystal', age: 29, gender: 'female' }
];

我试图根据对象的属性将一组对象组合在一起。我尝试使用_.groupBy,但它返回一个对象而不是一个数组。是否有类似的功能可以转换为以下结果?

期望的结果

var newPeople = [
  [
    { name: 'John', age: 30, gender: 'male' },
    { name: 'Dan',  age: 28, gender: 'male' }
  ],
  [
    { name: 'Kelly', age: 31, gender: 'female' },
    { name: 'Stephanie', age: 30, gender: 'female' },
    { name: 'Crystal', age: 29, gender: 'female' }
  ]
];

5 个答案:

答案 0 :(得分:3)

您可以使用groupBy然后使用values并获取数组数组。



var people = [
  { name: 'John', age: 30, gender: 'male' },
  { name: 'Kelly', age: 31, gender: 'female' },
  { name: 'Stephanie', age: 30, gender: 'female' },
  { name: 'Dan',  age: 28, gender: 'male' },
  { name: 'Crystal', age: 29, gender: 'female' }
];

var result = _.values(_.groupBy(people, 'gender'))
console.log(result)

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

您可以使用lodash&#39; s partition

代替groupBy
let result = _.partition(people, {gender: 'male'})

或者将它们放在单独的变量中,添加一些解构:

let [males, females] = _.partition(people, {gender: 'male'})

答案 2 :(得分:1)

您可以使用Set获取唯一的"gender"属性,.Array.from().filter()来创建已过滤匹配结果数组的数组

var people = [
  { name: 'John', age: 30, gender: 'male' },
  { name: 'Kelly', age: 31, gender: 'female' },
  { name: 'Stephanie', age: 30, gender: 'female' },
  { name: 'Dan',  age: 28, gender: 'male' },
  { name: 'Crystal', age: 29, gender: 'female' }
];


let res = Array.from(new Set(people.map(({gender}) => gender))
          , g => people.filter(({gender}) => gender === g));

console.log(res);

答案 3 :(得分:1)

您可以为组使用对象,并在使用组推送时迭代阵列。

var people = [{ name: 'John', age: 30, gender: 'male' }, { name: 'Kelly', age: 31, gender: 'female' }, { name: 'Stephanie', age: 30, gender: 'female' }, { name: 'Dan',  age: 28, gender: 'male' }, { name: 'Crystal', age: 29, gender: 'female' }],
    groups = Object.create(null),
    newPeople = [];
    
people.forEach(function (o) {
    if (!groups[o.gender]) {
        groups[o.gender] = [];
        newPeople.push(groups[o.gender]);
    }
    groups[o.gender].push(o);
});

console.log(newPeople);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 4 :(得分:1)

为什么不使用一个好的旧for循环?

var groups = {};
var newPeople = [];
var people = [
  { name: 'John', age: 30, gender: 'male' },
  { name: 'Kelly', age: 31, gender: 'female' },
  { name: 'Stephanie', age: 30, gender: 'female' },
  { name: 'Dan',  age: 28, gender: 'male' },
  { name: 'Crystal', age: 29, gender: 'female' }
];
for (let i = 0; i < people.length; i++) {
  let gender = people[i]["gender"];
  if (!groups[gender]) groups[gender] = [];
  groups[gender].push(people[i]);
}
for (let gender in groups) {
  newPeople.push(groups[gender]);
}
console.log(newPeople);