1

I have been struggling with this for the last two hours and still can not find a solution (maybe I am blocked!!).

I will start with how the result/output should looks like and is as follow:

results = [
  'result_1' => [
      "col_1" => "asc",
      "col_2" => "asc",
      "col_3" => "asc"
  ]
];

This is how I am doing it so far:

var ls_sort_keys = [];
var ls_settings = {};
var ls_report_name = "<?php echo $report_name ?>";

<?php foreach ($header_array as $key => $value) { ?>
   ls_sort_keys.push("<?php echo $value; ?>");
<?php } ?>

ls_settings[ls_report_name] = {
    $.each(ls_sort_keys, function (key, value) {
        console.log(key, value);
    });
}

The result of console.log(ls_sort_keys) is an array of three values containing ["col_1", "col_2", "col_3"].

I am getting the following error Uncaught SyntaxError: Unexpected token . in this line $.each(ls_sort_keys, function (key, value) and my guess is because is not allowed such thing in there.

Having said that, how I can transform the result into the output? The output is shown as an array but could be a Javascript object since it will be stored in the localStorage and AFAIK it does not support array but only strings.

10
  • Your PHP loop runs server-side, I do not see how it can be running the Javscript push(). (Except that last one) Commented May 17, 2018 at 14:42
  • Try assigning it to a property something like { items: $.each(...) } Commented May 17, 2018 at 14:43
  • Your desired result is invalid JavaScript. Also you cannot say console.log outputs something when you also say you have a syntax error. It cannot be both. This question lacks information needed to reproduce the issue. Commented May 17, 2018 at 14:44
  • @KevinBoucher what do you mean? Doing console.log(ls_sort_keys) I am able to see an array of three elements which is the expected result there. I am not following you. Commented May 17, 2018 at 14:47
  • 1
    @ReynierPM, ah I think I see now. Your loop outputs a succession of ls_sort_keys.push() calls that re subsequently processed on the client side. Commented May 17, 2018 at 14:54

1 Answer 1

1

Your final loop should indeed create an object. But the opening { will be interpreted as the start of an object literal, but then you go on as if it is the opening of a code block. This is invalid syntax.

Instead you can use Object.assign, computed property syntax and spread syntax, like this:

ls_settings[ls_report_name] = Object.assign(
    ...ls_sort_keys.map(col => ({ [col]: "asc" }))
)

Demo stripped from PHP code:

var ls_sort_keys = [];
var ls_settings = {};
var ls_report_name = "report_1";

ls_sort_keys.push("col_1", "col_2", "col_3");

ls_settings[ls_report_name] = Object.assign(
    ...ls_sort_keys.map(col => ({ [col]: "asc" }))
)

console.log(ls_settings);

Or, if you prefer a more old-fashioned syntax:

var ls_sort_keys = [];
var ls_settings = {};
var ls_report_name = "report_1";

ls_sort_keys.push("col_1", "col_2", "col_3");

var obj = {};
for (var i = 0; i < ls_sort_keys.length; i++) {
    obj[ls_sort_keys[i]] = "asc";
}

ls_settings[ls_report_name] = obj;

console.log(ls_settings);

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

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.