2017-05-09 35 views
1

我有一个JavaScript数组是这样的:的JavaScript - 分组物品放入数组

var items = [ 
{ id:1, group:'Produce', name:'Apple', weight: 0.5 }, 
{ id:2, group:'Produce', name:'Banana', weight: 0.2 }, 
{ id:3, group:'Meat', name:'Beef', weight: 1.0 }, 
{ id:4, group:'Meat', name:'Chicken', weight: 0.75 }, 
{ id:5, group:'Dairy', name:'Milk', weight:1.0 } 
]; 

我想通过这个数组的外观和动态的他们的小组把他们的阵列。我试过以下,但是,它没有工作:

var groups = []; 
for (var i = 0; i<items.length; i++) { 
    var groupName = items[i].group; 
    if (groups.includes(groupName) === false) { 
    groups[groupName] = new Array(); 
    } 
    groups[groupName].push(items[i]); 
} 

基本上,我试图在JavaScript中创建一个哈希表。关键是组名称,值是该组中项目的Array。但是,我一直没有成功。我在这里错过了什么?

非常感谢您的帮助!

+1

“它没有工作”和“我一直不成功”是不可接受的问题陈述。请明确定义问题,并使标题描述它。谢谢。 –

回答

0

您的groups应该是一个对象。

然后,您正在测试groups.includes(groupName),但它是an array function,并且您正在创建散列表。

而是检查对象是否存在与groups[groupName] === undefined

var groups = {}; 
for (var i = 0; i<items.length; i++) { 
    var groupName = items[i].group; 
    if (groups[groupName] === undefined) { 
    groups[groupName] = new Array(); 
    } 
    groups[groupName].push(items[i]); 
} 
1

我会建议你采用简单的解决方案。

var items = [ 
 
{ id:1, group:'Produce', name:'Apple', weight: 0.5 }, 
 
{ id:2, group:'Produce', name:'Banana', weight: 0.2 }, 
 
{ id:3, group:'Meat', name:'Beef', weight: 1.0 }, 
 
{ id:4, group:'Meat', name:'Chicken', weight: 0.75 }, 
 
{ id:5, group:'Dairy', name:'Milk', weight:1.0 } 
 
], obj = {}; 
 

 
items.forEach(c => (obj[c.group] || (obj[c.group] = [])).push(c)); 
 

 
console.log(obj);

0

如果该键存在,您可以使用只是一个对象和检查。如果不采取新阵列。

var items = [{ id: 1, group: 'Produce', name: 'Apple', weight: 0.5 }, { id: 2, group: 'Produce', name: 'Banana', weight: 0.2 }, { id: 3, group: 'Meat', name: 'Beef', weight: 1.0 }, { id: 4, group: 'Meat', name: 'Chicken', weight: 0.75 }, { id: 5, group: 'Dairy', name: 'Milk', weight:1.0 }], 
 
    groups = Object.create(null),   // take a really empty object 
 
    groupName, 
 
    i; 
 
    
 
for (i = 0; i < items.length; i++) { 
 
    groupName = items[i].group; 
 
    if (!groups[groupName]) {    // check if the key in object exists 
 
     groups[groupName] = [];   // if not, assign an empty array 
 
    } 
 
    groups[groupName].push(items[i]); 
 
} 
 

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

0

另一种方法是使用减少:

var items = [ 
 
{ id:1, group:'Produce', name:'Apple', weight: 0.5 }, 
 
{ id:2, group:'Produce', name:'Banana', weight: 0.2 }, 
 
{ id:3, group:'Meat', name:'Beef', weight: 1.0 }, 
 
{ id:4, group:'Meat', name:'Chicken', weight: 0.75 }, 
 
{ id:5, group:'Dairy', name:'Milk', weight:1.0 } 
 
]; 
 

 
const grouped = items.reduce((accum, item) => { 
 
\t accum[item.group] = accum[item.group] ? accum[item.group].concat(item) : [item]; 
 
\t return accum; 
 
}, {}); 
 

 
console.log(grouped);

0

我alwasy使用lodash或下划线

但如何永远,试试这个使用减少功能

var items = [ 
    { id:1, group:'Produce', name:'Apple', weight: 0.5 }, 
    { id:2, group:'Produce', name:'Banana', weight: 0.2 }, 
    { id:3, group:'Meat', name:'Beef', weight: 1.0 }, 
    { id:4, group:'Meat', name:'Chicken', weight: 0.75 }, 
    { id:5, group:'Dairy', name:'Milk', weight:1.0 } 
]; 

Array.prototype.groupDynamically = function(prop) { 
    return this.reduce(function(groups, item) { 
    var val = item[prop]; 
    groups[val] = groups[val] || []; 
    groups[val].push(item); 
    return groups; 
    }, {}); 
} 

//Then in your case say 

var groups = items.groupDynamically('group');