【发布时间】:2016-08-18 10:54:56
【问题描述】:
我需要根据 JavaScript 中的单个值动态拆分数组。
我有一个数组:
var dataStuff = [
{ Name: 'Apple', Tag: 'Fruit', Price: '2,5'},
{ Name: 'Bike', Tag: 'Sport', Price: '150'},
{ Name: 'Kiwi', Tag: 'Fruit', Price: '1,5'},
{ Name: 'Knife', Tag: 'Kitchen', Price: '8'},
{ Name: 'Fork', Tag: 'Kitchen', Price: '7'}
];
我希望数组按标签拆分, 例如。
var Fruit = [
{ Name: 'Apple', Tag: 'Fruit', Price: '2,5'},
{ Name: 'Kiwi', Tag: 'Fruit', Price: '1,5'}
];
var Sport = [
{ Name: 'Bike', Tag: 'Sport', Price: '150'}
];
var Kitchen = [
{ Name: 'Knife', Tag: 'Kitchen', Price: '8'},
{ Name: 'Fork', Tag: 'Kitchen', Price: '7'}
];
如果 dataStuff 数组中的 标签 更多,那么结果中的数组会更多。 无论如何,我不知道我该怎么做。我正在使用 node.js + Jade(用于查看),我认为最好的办法是在查看时执行此操作,因为我必须将每个数组放在 table 中。也许是这样的:
// Basic table
tbody
- each item in dataStuff
tr
td= item.Name
td= item.Tag
td= item.Price
// Other tables
- each item in dataStuff
item.Tag.push(item);
// adding items to array based on Tag
// probably it won't work
// but still how should i draw table?
如果有任何帮助,我将不胜感激
【问题讨论】:
标签: javascript arrays node.js pug