Suppose I have a dynamic array with an unspecified length, but two specific values are given for this array.
var arrName = ["firstName","lastName"];
I need to create a JSON variable that includes the exact values provided for this dynamic array. Here are my two approaches:
Method 1: Encounter Error Unexpected type '['
var jsonValues = {arrName[0] : "Mark", arrName[1]: "Collins"}
Method 2: This method works, but when I type 'jsonValues' in the console, it doesn't display the JSON contents; instead, it shows an Array type. I am unsure if this is acceptable because I intend to JSON.stringify it and send it via ajax to the PHP server.
var jsonValues = [{}];
jsonValues[arrName[0]] = "Jeffrey";
jsonValues[arrName[1]] = "Douglas";