2016-05-23 15 views
1

Ich habe eine Reihe von Objekten, und ich möchte in andere Objekte konvertieren.Was ist der optimale Weg, dies in Javascript zu tun?

var input = [ 
    { "type": "Pant", "brand": "A", "subBrand":"P", "size": "10"}, 
    {"type": "Pant", "brand": "A", "subBrand":"P", "size": "12"}, 
    {"type": "Pant", "brand": "A", "subBrand":"Q", "size": "12"}, 
    {"type": "Pant", "brand": "B", "subBrand":"P", "size": "10"}, 
    {"type": "Shirt", "brand": "A", "subBrand":"P", "size": "10"} 
]; 

Ausgabe sollte in diesem Format sein:

output = { 
    "Pant" : { 
     "A" : { 
      "P" : { 
      "size" : [10,12] 
      }, 
      "Q" : { 
      "size" : [12] 
      } 
     } 
     "B" : { 
      "P" : { 
      "size" : [10] 
      } 
     } 
    }, 
    "Shirt" : { 
     "A" : { 
      "P" : { 
      "size" : [10] 
      } 
     } 
    } 
}; 

Ich habe versucht, Code zu schreiben, und es ist sehr komplex geworden, jedes Mal, jedes Ding zu prüfen, ob seine früher kommen oder nicht. Bitte beraten.

Antwort

3

Sie könnten Array#forEach verwenden und das gewünschte Objekt mit einem leeren Standardobjekt erstellen.

var input = [{ "type": "Pant", "brand": "A", "subBrand": "P", "size": "10" }, { "type": "Pant", "brand": "A", "subBrand": "P", "size": "12" }, { "type": "Pant", "brand": "A", "subBrand": "Q", "size": "12" }, { "type": "Pant", "brand": "B", "subBrand": "P", "size": "10" }, { "type": "Shirt", "brand": "A", "subBrand": "P", "size": "10" }], 
 
    output = {}; 
 

 
input.forEach(function (a) { 
 
    output[a.type] = output[a.type] || {}; 
 
    output[a.type][a.brand] = output[a.type][a.brand] || {}; 
 
    output[a.type][a.brand][a.subBrand] = output[a.type][a.brand][a.subBrand] || { size: [] }; 
 
    output[a.type][a.brand][a.subBrand].size.push(a.size); 
 
}); 
 

 
console.log(output);

Wenn Sie es ein bisschen aufgeräumt (und in ES6) mögen, dann können Sie über die Tasten für das Objekt iterieren mit reduzieren und um das Objekt zu bauen.

var input = [{ "type": "Pant", "brand": "A", "subBrand": "P", "size": "10" }, { "type": "Pant", "brand": "A", "subBrand": "P", "size": "12" }, { "type": "Pant", "brand": "A", "subBrand": "Q", "size": "12" }, { "type": "Pant", "brand": "B", "subBrand": "P", "size": "10" }, { "type": "Shirt", "brand": "A", "subBrand": "P", "size": "10" }], 
 
    output = {}; 
 

 
input.forEach(function (a) { 
 
    var o = ['type', 'brand', 'subBrand'].reduce((r, k) => r[a[k]] = r[a[k]] || {}, output); 
 
    o.size = o.size || []; 
 
    o.size.push(a.size); 
 
}); 
 

 
console.log(output);

+1

Vielen Dank verwenden können, reduzieren war super !! – ajayv

+0

@ajayv, bitte schauen Sie hier: http://stackoverflow.com/help/someone-answers –

0

Sie .reduce

input.reduce((res,x)=>{ 
res[x.type] = res[x.type] || {}; 
res[x.type][x.brand] = res[x.type][x.brand] || {} 
res[x.type][x.brand][x.subBrand]= res[x.type][x.brand][x.subBrand] || {size:[]} 
res[x.type][x.brand][x.subBrand].size.push(x.size) 
return res; 
},{}) 
Verwandte Themen