0

I have an object structure like this:

{  
    "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
             }
          ]
       }
    ]
},

I want to change above structure to this one:

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

Basically I want to change options object.

"Options":

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

To this

"Blue":16.95,
"Red":55.95

Any JavaScript or lodash based solutions or suggestions are welcomed.

4 Answers 4

1

in plain Javascript, you could use two nested Array#forEach and build new properties and delete the options property at the end.

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

Sign up to request clarification or add additional context in comments.

Comments

0

Using lodash. I hope this helps you.

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>

Comments

0

You can probably use a constructor to reconstruct the object in the way you want.


So, you have this 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
        }
      ]
    }
  ]
};

And you want this object:

[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"
}

You can use a constructor -- everything you want to keep is referred to, and the things you want to move somewhere else are passed.

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

Hope this helped!

Comments

0

You could also do this with JS alone.

Here is what you could do.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.