2016-11-18 56 views
0

我有一個對象的結構是這樣的舊數組:從對象數組嵌套對象結構更改爲一個共同的對象和刪除對象

{ 
    "name":"Garden", 
    "live":true, 
    "isUpdated":true, 
    "categories":[ 
     { 
      "min":0, 
      "max":0, 
      "required":true, 
      "category":"flower", 
      "options":[ 
      { 
       "optionName":"Blue", 
       "optionValue":16.95 
      }, { 
       "optionName":"Red", 
       "optionValue":55.95 
      } 
      ] 
     } 
    ] 
}, 

我想上述結構改變到這一個:

{ 
    "name":"Garden", 
    "live":true, 
    "isUpdated":true, 
    "categories":[ 
     { 
      "min":0, 
      "max":0, 
      "required":true, 
      "category":"flower", 
      "Blue":16.95, 
      "Red":55.95 
     } 
    ] 
} 

基本上我想改變選項對象。

「選項」:

[ 
    { 
     "optionName":"Blue", 
     "optionValue":16.95 
    }, { 
     "optionName":"Red", 
     "optionValue":55.95 
    } 
] 

對此

"Blue":16.95, 
"Red":55.95 

JavaScript或基於lodash解決方案或建議,歡迎。

回答

0

使用lodash。 我希望這可以幫助你。

var mObj = { 
 
    "name":"Garden", 
 
    "live":true, 
 
    "isUpdated":true, 
 
    "categories":[ 
 
     { 
 
      "min":0, 
 
      "max":0, 
 
      "required":true, 
 
      "category":"flower", 
 
      "options":[ 
 
      { 
 
       "optionName":"Blue", 
 
       "optionValue":16.95 
 
      }, 
 
      { 
 
       "optionName":"Red", 
 
       "optionValue":55.95 
 
      } 
 
      ] 
 
     } 
 
    ] 
 
}; 
 
       
 
mObj.categories.forEach(function(category){ 
 
     var transformedOption = _.transform(category.options, function(result, obj) { 
 
      return _.assign(result, { [obj.optionName] : obj.optionValue}); 
 
     }, {}); 
 
category = _.assign(category, transformedOption); 
 
_.unset(category, "options"); 
 
}) 
 

 
console.log(mObj);
<script src="https://cdn.jsdelivr.net/lodash/4.17.2/lodash.js"></script>

0

你或許可以使用構造函數來重建你想要的方式的對象。


所以,你有這樣的obect:

var oldObject = { 
    "name":"Garden", 
    "live":true, 
    "isUpdated":true, 
    "categories":[ 
    { 
     "min":0, 
     "max":0, 
     "required":true, 
     "category":"flower", 
     "options":[ 
     { 
      "optionName":"Blue", 
      "optionValue":16.95 
     }, 
     { 
      "optionName":"Red", 
      "optionValue":55.95 
     } 
     ] 
    } 
    ] 
}; 

而你要這個對象:

[object Object] { 
    categories: [[object Object] { 
    blue: 16.95, 
    category: "flower", 
    max: 0, 
    min: 0, 
    red: 55.95, 
    required: true 
}], 
    isUpdated: true, 
    live: true, 
    name: "Garden" 
} 

您可以使用一個構造函數 - 你想保留一切被稱爲,而你想移動到別的地方的東西都會通過。

function newObject(blue_value, red_value) { 
    this.name = oldObject.name; 
    this.live = oldObject.live; 
    this.isUpdated = oldObject.isUpdated; 
    this.categories = [ 
    { 
     min: oldObject.categories[0].min, 
     max: oldObject.categories[0].max, 
     required: oldObject.categories[0].required, 
     category: oldObject.categories[0].category, 
     blue: blue_value, 
     red: red_value 
    } 
    ]; 
} 

希望這有助於!

1

使用純Javascript時,可以使用兩個嵌套的Array#forEach並構建新屬性並在末尾刪除options屬性。

var object = { name: "Garden", live: true, sUpdated: true, categories: [{ min: 0, max: 0, required: true, category: "flower", options: [{ optionName: "Blue", optionValue: 16.95 }, { optionName: "Red", optionValue: 55.95 }] }] }; 
 

 
object.categories.forEach(function (c) { 
 
    c.options.forEach(function (o) { 
 
     c[o.optionName] = o.optionValue; 
 
    }); 
 
    delete c.options; 
 
}); 
 

 
console.log(object);
.as-console-wrapper { max-height: 100% !important; top: 0; }

0

你也可以用JS單獨做到這一點。

這是你可以做的。

var input = { 
 
    "name": "Garden", 
 
    "live": true, 
 
    "isUpdated": true, 
 
    "categories": [{ 
 
    "min": 0, 
 
    "max": 0, 
 
    "required": true, 
 
    "category": "flower", 
 
    "options": [{ 
 
     "optionName": "Blue", 
 
     "optionValue": 16.95 
 
    }, { 
 
     "optionName": "Red", 
 
     "optionValue": 55.95 
 
    }] 
 
    }] 
 
}; 
 

 
input.categories.forEach((category) => { 
 
    let options = category.options; 
 
    delete category["options"]; 
 
    let output = options.forEach((item) => { 
 
    category[item.optionName] = item.optionValue 
 
    }); 
 

 
    console.log(input); 
 
});