0

i have a json string in the format

var jsonData =    [{"label":"Hass1([email protected])","value":"[email protected]"},{"label":"Hass([email protected])","value":"[email protected]"},{"label":"Sam([email protected])","value":"[email protected]"}]

i need to convert it in the format it into this way

var obj = {"Hass1([email protected])":"[email protected]",
          "Hass([email protected])","[email protected]"}

how to do so?

I have implemented so far like

function ConvertMeJason(jsonMe) {
            var list = JSON.parse(jsonMe);
            list.Object.forEach(function (obj) { /// I am getting error undefined function foreach
                emptyJson.add('"' + obj.label + '"', '"' + obj.value + '"');
            })
        }
Hassaan
  • 3,931
  • 11
  • 34
  • 67

1 Answers1

0

Something like this:

function convert(data) {
    var obj = {};
    for (var i = 0; i<data.length; i++) {
        obj[data[i].label] = data[i].value;
    }
    return obj;
}

Here is a JSFiddle.

Corey
  • 5,818
  • 2
  • 24
  • 37