-1

I want to split the JSON format data as key,value pair of data without using map.Please find the below json data.

var data={"123":"test1","2365":"test2","1233":"test3","112365":"test4"}

I want to split as like below output :

key : 123 value : test1 key : 2365 value : test2

 success: function (data) {
        $.each(response.data, function(value,key) {
        return {label: key+","+value,value: key,desc : value};
        });
user2848031
  • 187
  • 12
  • 36
  • 69

2 Answers2

0

As per the documentation, the order for the callback function variables is key, value not value, key. You're returning at the first iteration of your each loop also, perhaps you should add to an array in the loop and return the array at the end? You've also got response.data which I assume is probably data.response?

success: function(data) {
    var output = [];
    $.each(data.response, function(key, value) {
        output.push([key, value]);
    });
    return output;
}

That's just an example, obviously you'll want to restructure that array so that you can actually use the key and value...

Here's a jsFiddle example for this

scrowler
  • 24,273
  • 9
  • 60
  • 92
0

Are you looking for something like this?

    var data = { "123": "test1", "2365": "test2", "1233": "test3", "112365": "test4" };

    var result = "";
    for (var key in data)
    {
        result += "key:  " + key + "  value: " + data[key] + "  ";
    };

    alert(result);  
Steve Wellens
  • 20,506
  • 2
  • 28
  • 69