【问题标题】:How to split array dynamically based on single value in JavaScript Node.js如何在 JavaScript Node.js 中基于单个值动态拆分数组
【发布时间】: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


    【解决方案1】:

    如果您的标签名称是事先已知且有限的

    那么简单

    var Fruit = dataStuff.filter(function(val){
      return val.Tag == "Fruit";
    });
    var Sport = dataStuff.filter(function(val){
      return val.Tag == "Sport";
    });
    var Kitchen = dataStuff.filter(function(val){
      return val.Tag == "Kitchen";
    });
    

    或者您可以创建一个 JSON 对象来保留标签名称,例如

    var tags = {
      "Fruit" : [],
      "Sport" : [],
      "Kitchen" : [],
    };
    for(var tag in tags)
    {
       tags[tag] = dataStuff.filter(function(val){
          return val.Tag == tag;
        });  
    }
    

    现在tags.Fruit 将为您提供Fruit 数组。

    【讨论】:

    • 不,他们不知道。这就是为什么,它必须是动态的。
    • @DiPix 我也为此提供了一个选项。
    • 您的解决方案微不足道且无用 - op 使用“动态”一词
    【解决方案2】:

    您可以将对象与分组项目一起使用。它适用于任何标签,并允许列出带有Object.keys(grouped) 的所有标签(如果需要)。

    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>');

    【讨论】:

    • 拥有一个完全空的对象有什么帮助? Object.create(null)
    • 因为你可以有一个标签toString,这是行不通的。
    • 如果我的 dataStuff 来自数据库怎么办?那我该如何使用grouped = Object.create(null);var groupedstuff = dataStuff, grouped = Object.create(null); - 可能行不通?
    • @Nina Scholz 你的意思是grouped[a.tag]toString() 不起作用吗?还是“[object Object]”不应该出现?
    • @AmreshVenugopal, grouped['toString'].push(...) 不起作用,因为 grouped.toString 是一个函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-04
    • 2023-02-20
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多