将嵌套对象结构从对象数组更改为公共对象,并删除旧的对象数组

时间:2016-11-18 07:46:44

标签: javascript json object lodash

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

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

4 个答案:

答案 0 :(得分: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; }




答案 1 :(得分: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>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

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

所以,你有这个对象:

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

希望这有帮助!

答案 3 :(得分:0)

你也可以单独用JS做这件事。

这是你能做的。

&#13;
&#13;
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);
});
&#13;
&#13;
&#13;