【问题标题】:Change the nested object structure from array of objects to a common object and remove old array of objects将嵌套对象结构从对象数组更改为公共对象并删除旧的对象数组
【发布时间】:2016-11-18 07:46:43
【问题描述】:

我有一个这样的对象结构:

{  
    "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 的解决方案或建议。

【问题讨论】:

    标签: javascript json object lodash


    【解决方案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; }

    【讨论】:

      【解决方案2】:

      使用 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>

      【讨论】:

        【解决方案3】:

        您可能可以使用构造函数以您想要的方式重构对象。


        所以,你有这个对象:

        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
            }
          ];
        }
        

        希望这有帮助!

        【讨论】:

          【解决方案4】:

          你也可以单独使用 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);
          });

          【讨论】:

            猜你喜欢
            • 2021-12-28
            • 1970-01-01
            • 2020-12-18
            • 2020-12-07
            • 1970-01-01
            • 1970-01-01
            • 2019-04-26
            • 2020-04-15
            • 2020-08-02
            相关资源
            最近更新 更多