JSON复杂的嵌套数组使用顺序排序

时间:2019-04-08 09:36:45

标签: javascript json sorting

我有一个复杂的嵌套JSON数组,我想使用外部级别的itemsortorder和内部级别的subitemnameorder从内部和外部对其进行排序

我下面的给定数组


    id:1,
    "fruits":[
        {
        "itemid":1,
        "itemname":"Red Fruits",
        "itemsortorder":2,
        "subitems":[ {"subitemname":"Apple", "subitemnameorder":2},
                     {"subitemname":"Grapes", "subitemnameorder":1},
                     {"subitemname":"Berryberry", "subitemnameorder":3}
                   ]
        },
        {
        "itemid":2,
        "itemname":"Yellow Fruits",
        "itemsortorder":1,
        "subitems":[ {"subitemname":"Orange", "subitemnameorder":3},
                     {"subitemname":"Gua", "subitemnameorder":1},
                     {"subitemname":"Mango", "subitemnameorder":2}
                   ]
        }

    ],
    "Vegetable":[
        {
        "itemid":3,
        "itemname":"Red Veggies",
        "itemsortorder":2,
        "subitems":[ {"subitemname":"Onion", "subitemnameorder":2},
                     {"subitemname":"Ginger", "subitemnameorder":1},
                     {"subitemname":"Carrot", "subitemnameorder":3}
                   ]
        },
        {
        "itemid":4,
        "itemname":"Small Veggies",
        "itemsortorder":1,
        "subitems":[ {"subitemname":"pepper", "subitemnameorder":3},
                     {"subitemname":"halo", "subitemnameorder":1},
                     {"subitemname":"good", "subitemnameorder":2}
                   ]
        },
        {
        "itemid":5,
        "itemname":"Cool",
        "itemsortorder":3,
        "subitems":[ {"subitemname":"abc", "subitemnameorder":3},
                     {"subitemname":"pqr", "subitemnameorder":1},
                     {"subitemname":"kooi", "subitemnameorder":2}
                   ]
        }

    ],

    "avoid":[
    {
        "itemid":3,
        "itemname":"avoid Veggies",
        "itemsortorder":2,
        "subitems":[ {"subitemname":"Onion", "subitemnameorder":2},
                     {"subitemname":"Ginger", "subitemnameorder":1},
                     {"subitemname":"Carrot", "subitemnameorder":3}
                   ]
        }]
}

避免使用规避数组部分

预期


   {  
      "filename":"Yellow Fruits",
      "children":[  
         {  
            "filename":"Gua",
            "subitemnameorder":1
         },
         {  
            "filename":"Mango",
            "subitemnameorder":2
         },
         {  
            "filename":"Orange",
            "subitemnameorder":3
         }
      ]
   },
   {  
      "filename":"Red Fruits",
      "children":[  
         {  
            "filename":"Grapes",
            "subitemnameorder":1
         },
         {  
            "filename":"Apple",
            "subitemnameorder":2
         },
         {  
            "filename":"Berryberry",
            "subitemnameorder":3
         }
      ]
   },
   ,
   {  
      "filename":"Small Veggies",
      "children":[  
         {  
            "filename":"halo",
            "subitemnameorder":1
         },
         {  
            "filename":"good",
            "subitemnameorder":2
         },
         {  
            "filename":"pepper",
            "subitemnameorder":3
         }
      ]
   },
   {  
      "filename":"Red Veggies",
      "children":[  
         {  
            "filename":"Ginger",
            "subitemnameorder":1
         },
         {  
            "filename":"Carrot",
            "subitemnameorder":2
         },
         {  
            "filename":"Onion",
            "subitemnameorder":3
         }
      ]
   },
   {  
      "filename":"Cool",
      "children":[  
         {  
            "filename":"pqr",
            "subitemnameorder":1
         },
         {  
            "filename":"kooi",
            "subitemnameorder":2
         },
         {  
            "filename":"abc",
            "subitemnameorder":3
         }
      ]
   }
   ]

首先应从水果中提取并进行相应的分类,然后从蔬菜中获取并进行跟踪

我尝试使用过滤器和地图,但无法正常工作。请帮忙。

我尝试过下面的功能,请看小提琴

https://jsfiddle.net/qb2pueg7

1 个答案:

答案 0 :(得分:3)

我假设您将json分配给变量data,因此代码如下所示

let keysarr = Object.keys(data);
let temp = [];
  for(let i=0;i<keysarr.length;i++){
    if(keysarr[i]!=="id" && keysarr[i]!=="avoid"){
     let tempsortL1 = data[keysarr[i]].
      sort((a,b)=>a.itemsortorder-b.itemsortorder).map((item)=>{
                let retObj={};
              retObj["filename"] = item.itemname;
              retObj["children"]=item.subitems.sort((a,b)=>a.subitemnameorder-b.subitemnameorder); 
          temp.push(retObj);
          return retObj;});

 }
};

您所需的数组将到达temp。抱歉,如果您认为代码未格式化,我试图保持在排序时不考虑键。 这是小提琴https://jsfiddle.net/pv2sLejc/ 还要附加控制台的图像enter image description here