我有一个类似这样的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中创建一个Hashtable。关键是组名称,值为该组中项目的Array
。但是,我没有成功。我在这里缺少什么?
非常感谢你的帮助!
答案 0 :(得分: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);
&#13;
答案 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]);
}
答案 2 :(得分: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; }
答案 3 :(得分:0)
另一种选择是使用reduce:
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) => {
accum[item.group] = accum[item.group] ? accum[item.group].concat(item) : [item];
return accum;
}, {});
console.log(grouped);
&#13;
答案 4 :(得分:0)
我alusty使用lodash或下划线
但是,请尝试使用reduce函数
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');