0

How can I get the results using $each outside of the scope:

$('body').on('click', 'a.action', function() {
    var dataresult = $(this).data("set");

    $.each(dataresult.page[0], function(key, value) {
        //do something here
    });

    //Result needs to go here in this format
    "key": value,
    "key1": value1,
    "key2": value2,



});

Does jQuery have something like implode?

Alko
  • 1,421
  • 5
  • 25
  • 51
  • push the key value pair in an array. – Akki619 Sep 10 '15 at 15:43
  • possible duplicate of [What is the scope of variables in JavaScript?](http://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript) – Andy Hoffner Sep 10 '15 at 16:02
  • @Akki619 thanks but it does not work for me. The next part of the code is $load.("page.php",{ }), and If I place dataresult within {} The error in console is "SyntaxError: missing :" – Alko Sep 10 '15 at 16:05
  • why you are putting dataresult within {}. Update question with what next thing you are doing in your code. – Akki619 Sep 10 '15 at 16:10
  • Removing {} worked fine. Thanks/ – Alko Sep 10 '15 at 17:07

1 Answers1

0

Push value in an array like below code.

$('body').on('click', 'a.action', function() {
    var dataresult = $(this).data("set");
    var arr = [];
    $.each(dataresult.page[0], function(key, value) {
        arr[key]=value;
    });

});
Akki619
  • 2,386
  • 6
  • 26
  • 56