本文介绍了如何动态地根据在JavaScript中的Node.js单个值分割阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要动态的基于JavaScript中单个值分割阵列。

我有一个数组:

  VAR dataStuff = [
    {名称:'苹果',标签:'果',价钱:2,5},
    {名称:'自行车',标签:运动,价格:150},
    {名称:'猕猴桃',标签:'果',价钱:1,5},
    {名称:'刀',标签:'厨房',价钱:'8'},
    {名称:'叉',标签:'厨房',价钱:'7'}
];

,我希望通过标签拆分阵列,
例如:

  VAR水果= [
    {名称:'苹果',标签:'果',价钱:2,5},
    {名称:'猕猴桃',标签:'果',价钱:'1,5'}
];VAR运动= [
    {名称:'自行车',标签:运动,价格:'150'}
];VAR厨房= [
    {名称:'刀',标签:'厨房',价钱:'8'},
    {名称:'叉',标签:'厨房',价钱:'7'}
];

如果在dataStuff阵列将更多的标签,然后在结果将是更多的阵列。
反正我没有想法,我应该怎么做。我使用的Node.js +玉(对于视图),我认为最好的办法将在视图,因为我已经把在表中的每个阵列来做到这一点。也许是这样的:

  //基本表
TBODY
      - 在dataStuff每个项目
         TR
            TD = item.Name
            TD = item.Tag
            TD = item.Price//其他表
- 在dataStuff每个项目
    item.Tag.push(项目);
    //基于标签项目添加到阵列
    //可能将无法正常工作
    //但仍然应该怎么画表?

我会感谢任何帮助


解决方案

您可以使用一个对象与分组的项目。它适用于任何标记,并允许所有标签的​​列表,包括 Object.keys(分组),如果需要的话。

\r
\r

VAR dataStuff = [{名称:'苹果',标签:'果',价格:2,5},{名称:'自行车',标签:运动,价格:'150'},{名称:'猕猴桃',标签:'果',价钱:'1,5'} {名称:'刀',标签:'厨房',价钱:'8'},{名称:'叉',标签:'厨房',价钱:'7'}],\r
    分组=的Object.create(NULL);\r
\r
dataStuff.forEach(函数(){\r
    分组[a.Tag] =分组[a.Tag] || [];\r
    分组[a.Tag] .push(一);\r
});\r
\r
的document.write(Object.keys(分组));\r
的document.write('< pre>'+ JSON.stringify(分组,0,4)+'< / pre>');

\r

\r
\r

I need to split array dynamically based on single value in JavaScript.

I've got an array:

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'}
];

And i expect arrays split by Tag,eg.

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'}
];

If in dataStuff array will be more Tags then in result will be more arrays.Anyway i don't have idea how should I do this. I'm using node.js + Jade (for view), and i think the best idea will be do this at view because i have to put each array in table. Maybe something like this:

// 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?

I would be grateful for any help

解决方案

You could use an object with the grouped items. It works for any tags and allows a list of all tags with Object.keys(grouped), if required.

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' }],
    grouped = Object.create(null);

dataStuff.forEach(function (a) {
    grouped[a.Tag] = grouped[a.Tag] || [];
    grouped[a.Tag].push(a);
});

document.write(Object.keys(grouped));
document.write('<pre>' + JSON.stringify(grouped, 0, 4) + '</pre>');

这篇关于如何动态地根据在JavaScript中的Node.js单个值分割阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 11:20