Re-Structuring a JSON

后端 未结 2 874
鱼传尺愫
鱼传尺愫 2020-12-22 08:30

OK guys need some help out there. I\'m still learning Javascript and its interactions JSON. I have a a JSON like this

[{
  \"categories\":\"Food\",
  \"subca         


        
相关标签:
2条回答
  • 2020-12-22 09:13

    I workout a solution, but I'm not sure how it will handle big json data. Assume that the json var stores your data:

    categories = [];
    json.forEach(function(entry) {  
        var cindex = categories.map(function(category) { 
            return category.name; 
        }).indexOf(entry.categories);
    
        if (cindex < 0) {
            // Not found in categories array
            cindex = categories.push({
                name: entry.categories,
                subcategories: []
            }) - 1; // -1 to fix the index
        }
    
        // Lets search the subcategory
        var category = categories[cindex];
    
        var sindex = category.subcategories.map(
            function(subcategory) {
                return subcategory.name;
            }
        ).indexOf(entry.subcategories);
    
        if (sindex < 0) {
          // Not Found
            sindex = category.subcategories.push({
                name: entry.subcategories,
                items: []
            }) - 1; 
        } 
        // Subcategory exists. Just push
        category.subcategories[sindex].items.push({
            pid: entry.pid,        
            description: entry.description,
            title: entry.title
        });
    });
    
    var menu = {
        menu: {
            categories: categories
        }
    };
    

    Working Fiddle

    0 讨论(0)
  • 2020-12-22 09:28

    I gave this a shot and might as well post my results. The data doesn't line up perfectly with what you have. It doesn't require jQuery, and it can be used to sort data multiple times.

    Object.prototype.groupBy = function(itemName, preGrouped)
    {
        if(preGrouped)
        {
            for(prop in this)
            {
                if(typeof(this[prop]) === 'object' && this[prop].groupBy !== undefined)
                {
                    var reGroup = this[prop].groupBy(itemName);  
                    this[prop] = reGroup;
                }
            }
            return this;
        }
        else
        {
            var uniqueItems = {};
            var uniqueItemLength = 0;
            for(var i=0, length=this.length; i < length; i++)
            {
               var item = this[i]; 
               var z =0;
               var found = false;
               while(z < uniqueItemLength)
               {     
                   if(item[itemName] in uniqueItems)
                   {
                       uniqueItems[item[itemName]].push(item);
                       found = true;
                       break;
                   }
                   z++;
               }
                if(!found)
                {
                    uniqueItems[item[itemName]] = [];
                    uniqueItems[item[itemName]].push(item); 
                    uniqueItemLength++;
                }      
            }
            return uniqueItems;
        } 
    }
    

    It gets called by:

    var convertedData = oldJSON.groupBy('categories').groupBy('subcategories', true)
    

    Beterraba's answer is correct. I put some time in this one and figured I'd at least post. Maybe it would help someone else.

    http://jsfiddle.net/8VRuy/

    0 讨论(0)
提交回复
热议问题