Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript,按属性提取和组织对象数组_Javascript_Vue.js - Fatal编程技术网

Javascript,按属性提取和组织对象数组

Javascript,按属性提取和组织对象数组,javascript,vue.js,Javascript,Vue.js,简而言之,我想创建一个通用方法“organize”来实现以下目的: [ { name: 'band1', type: 'concert', subtype: 'rock' }, { name: 'band2', type: 'concert', subtype: 'jazz' }, { name: 'band3', type: 'concert', subtype: 'jazz' }, { name: 'fun1', type: 'festival', subty

简而言之,我想创建一个通用方法“organize”来实现以下目的:

[
    { name: 'band1', type: 'concert', subtype: 'rock' },
    { name: 'band2', type: 'concert', subtype: 'jazz' },
    { name: 'band3', type: 'concert', subtype: 'jazz' },
    { name: 'fun1', type: 'festival', subtype: 'beer' },
    { name: 'fun2', type: 'festival', subtype: 'food' },
]
为此:

{
    'concert': {
      'rock': [
        { name: 'band1', type: 'concert', subtype: 'rock' },
      ],
      'jazz': [
        { name: 'band2', type: 'concert', subtype: 'jazz' },
        { name: 'band3', type: 'concert', subtype: 'jazz' }
      ]
    },
    'festival': {
      'beer': [
        { name: 'fun1', type: 'festival', subtype: 'beer' },
      ],
      'food': [
        { name: 'fun2', type: 'festival', subtype: 'food' },
      ]
    }
}
签名如下:

organize(events, ['type', 'subtype']);
我想这样做的原因是有一个简单的嵌套for循环来显示数据。如果您熟悉Vue.js,它将如下所示:

<div v-for="(type, subtypes) in organize(events, ['type', 'subtype'])">
  <h1>{{ type }}</h1>
  <div v-for="(subtype, events) in subtypes">
    <h2>{{ subtype }}</h2>
    <div v-for="event in events">
      <h3>{{ event.name }}</h3>
    </div>
  </div>
</div>

{{type}}
{{subtype}}
{{event.name}
对我来说,这是一次脑力激荡的锻炼。我认为递归和
map
filter
reduce
的一些组合已经成熟,但我花了太多时间试图想出一个解决方案,而是编写了一些非常难看的代码来完成本质上相同的事情。也有可能我不知道javascript的一些有用的特性可以帮助我做到这一点


建议或指导将不胜感激,但这不是当务之急。我只是认为有一个通用的方法来组织这样的数组是非常有用的。

我认为这可能是
组织的一个正确实现:

功能组织(行、分组){
var last=groupBy.length-1;
返回rows.reduce((res,obj)=>{
groupBy.reduce((res,grp,i)=>
res[obj[grp]]| |(res[obj[grp]]=i==last?[]:{},res);
返回res;
}, {});
}
var事件=[
{名称:'band1',类型:'concert',子类型:'rock'},
{名称:'band2',类型:'concert',子类型:'jazz'},
{名称:'band3',类型:'concert',子类型:'jazz'},
{名称:'fun1',类型:'festival',子类型:'beer'},
{名称:'fun2',类型:'festival',子类型:'food'},
];
var res=组织(事件,['type','subtype']);
控制台日志(res)您可以从库中使用:

var源=[
{名称:'band1',类型:'concert',子类型:'rock'},
{名称:'band2',类型:'concert',子类型:'jazz'},
{名称:'band3',类型:'concert',子类型:'jazz'},
{名称:'fun1',类型:'festival',子类型:'beer'},
{名称:'fun2',类型:'festival',子类型:'food'},
];
var groupedByType=u.groupBy(源,“类型”);
console.log(groupedByType);
var groupedByTypeAndSubtype={};
for(groupedByType中的var i){
groupedByTypeAndSubtype[i]=u.groupBy(groupedByType[i],'subtype');
}
log(groupedByTypeAndSubtype)

只是一个建议:如果您已经有了一个可行的解决方案,但您认为可以进一步优化,那么您可以将问题发布在;)干得好!今晚我没有时间看它,但是把你的函数复制粘贴到我的代码中,效果非常好!干净多了。我很高兴以后再看一遍。我有一种预感reduce会很有用,但我总是忽略它而选择map和filter。谢谢