Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.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 - Fatal编程技术网

如何在javascript中将列表转换为包含子项的类别?

如何在javascript中将列表转换为包含子项的类别?,javascript,Javascript,如何将中的以下内容转换为expectedResult const items = [ { type: 'fruits', index: 1, name: 'FRUITS' }, { type: 'fruit', index: 2, name: 'orange' }, { type: 'fruit', index: 3, name: 'apple' }, { type: 'fruit', index: 4, name: 'banana' },

如何将中的以下内容转换为expectedResult

const items = [
      { type: 'fruits', index: 1, name: 'FRUITS' },
      { type: 'fruit', index: 2, name: 'orange' },
      { type: 'fruit', index: 3, name: 'apple' },
      { type: 'fruit', index: 4, name: 'banana' },

      { type: 'vegetables', index: 5, name: 'VEGETABLES' },
      { type: 'vegetable', index: 6, name: 'ginger' },
      { type: 'vegetable', index: 7, name: 'tomato' },
      { type: 'vegetable', index: 8, name: 'potato' },
      { type: 'vegetable', index: 9, name: 'garlic' },
      { type: 'vegetable', index: 10, name: 'cucumber' },

      { type: 'meats', index: 11, name: 'MEATS' },
      { type: 'meat', index: 12, name: 'Beef' },
      { type: 'meat', index: 13, name: 'Chicken' },
      
   ...so on
    ];
为此:

    const expectedResult = [
      {
        type: 'fruits',
        index: 1,
        name: 'FRUITS',
        items: [
          { type: 'fruit', index: 2, name: 'orange' },
          { type: 'fruit', index: 3, name: 'apple' },
          { type: 'fruit', index: 4, name: 'banana' },
        ],
      },

      {
        type: 'vegetables',
        index: 5,
        name: 'VEGETABLES',
        items: [
          { type: 'vegetable', index: 6, name: 'ginger' },
          { type: 'vegetable', index: 7, name: 'tomato' },
          { type: 'vegetable', index: 8, name: 'potato' },
          { type: 'vegetable', index: 9, name: 'garlic' },
          { type: 'vegetable', index: 10, name: 'cucumber' },
        ],
      },

      {
        type: 'meats',
        index: 11,
        name: 'MEATS',
        items: [
          { type: 'meat', index: 12, name: 'Beef' },
          { type: 'meat', index: 13, name: 'Chicken' },
        ],
      },
         ...so on
    ];
因此,我们的想法是查找
类型
,然后查找下一个索引,直到索引更改
类型
,并将这些索引移动到子数组中,如
项:[]

例如,对于项目
类型:“水果”
检查连续进行的索引,直到类型更改为
类型:“蔬菜”
,并将索引之间的索引复制到
类型:“水果”,项目:[……在此数组中]
数组


**其思想是寻找连续的中间索引,并将它们复制到
项中:[
子数组。

您可以将这些项像这样分成两个数组,然后从那里开始。。。
const项=[
{类型:'fruits',索引:1,名称:'fruits'},
{类型:'fruit',索引:2,名称:'orange'},
{类型:'fruit',索引:3,名称:'apple'},
{类型:'水果',索引:4,名称:'香蕉'},
{类型:'蔬菜',索引:5,名称:'蔬菜'},
{类型:'蔬菜',索引:6,名称:'生姜'},
{类型:'蔬菜',索引:7,名称:'番茄'},
{类型:'蔬菜',索引:8,名称:'马铃薯'},
{类型:'蔬菜',索引:9,名称:'大蒜'},
{类型:'蔬菜',索引:10,名称:'黄瓜'},
{类型:'meat',索引:11,名称:'meat'},
{类型:'蔬菜',索引:12,名称:'牛肉'},
{类型:'蔬菜',索引:13,名称:'鸡肉'},
];
常量组=项目。减少((acc,it)=>{
if(it.type.toUpperCase()==it.name){
附件猫推(it)
}否则{
acc.SUB.push(it)
}
返回acc;
},{cats:[],subs:[]});

console.log(组)
循环浏览项目。设置当前项目类型并将项目添加到预期列表中。与该类型匹配的后续项将添加到该项中。否则重复

const项=[
{类型:'fruits',索引:1,名称:'fruits'},
{类型:'fruit',索引:2,名称:'orange'},
{类型:'fruit',索引:3,名称:'apple'},
{类型:'水果',索引:4,名称:'香蕉'},
{类型:'蔬菜',索引:5,名称:'蔬菜'},
{类型:'蔬菜',索引:6,名称:'生姜'},
{类型:'蔬菜',索引:7,名称:'番茄'},
{类型:'蔬菜',索引:8,名称:'马铃薯'},
{类型:'蔬菜',索引:9,名称:'大蒜'},
{类型:'蔬菜',索引:10,名称:'黄瓜'},
{类型:'meats',索引:11,名称:'meats'},
{类型:'meat',索引:12,名称:'Beef'},
{类型:'meat',索引:13,名称:'Chicken'},
];
var类型,预期=[];
对于(变量i=0;i如果(type&&0您可以使用reduce函数重新格式化对象:

items.reduce((acc,item)=>{
const availableTypes=Object.keys(acc)
如果(!acc[item.type]&&&!availableTypes.some((type)=>type.indexOf(item.type)==0)){
return acc[item.type]={…item,items:[]}
}否则{
const currentType=availableTypes.find((类型)=>type.indexOf(item.type)==0)
返回acc[当前类型]={
…acc[当前类型],
项目:acc[currentType].items.concat(项目)
}
}
}, {})
const项=[
{类型:'fruits',索引:1,名称:'fruits'},
{类型:'fruit',索引:2,名称:'orange'},
{类型:'fruit',索引:3,名称:'apple'},
{类型:'水果',索引:4,名称:'香蕉'},
{类型:'蔬菜',索引:5,名称:'蔬菜'},
{类型:'蔬菜',索引:6,名称:'生姜'},
{类型:'蔬菜',索引:7,名称:'番茄'},
{类型:'蔬菜',索引:8,名称:'马铃薯'},
{类型:'蔬菜',索引:9,名称:'大蒜'},
{类型:'蔬菜',索引:10,名称:'黄瓜'},
{类型:'MEAT',索引:11,名称:'MEAT'},
{类型:'meat',索引:12,名称:'Beef'},
{类型:'meat',索引:13,名称:'Chicken'}
];
让expectedResult=[];
items.forEach(功能(元素){
var parentItem=expectedResult.findIndex(ele=>ele.type==element.type+'s');
如果(父项>=0){
expectedResult[parentItem].items.push(元素);
返回;
}
element.items=[];
expectedResult.push(元素);
});

console.log(expectedResult);
只需循环您的数组并构造新的数组,如何查找下一次遇到的类型更改副本,即
项:[]
sub-array?父项和子项具有相同的键,唯一的区别在于大小写和复数形式,建议使用更好的键进行区分,否则对于庞大的数据集可能会出现错误。这是否回答了您的问题?