This question already has an answer here:
I have a string returned from php ajax page like this:
$returnString = "firstName=" . $row['firstName'] . "&lastName="
.$row['lastName'] . "&address=" . $row['address'] . "&province_id="
.$row['province_id'] . "&city_id=" . $row['city_id'];
so on my calling page I get the string returned to :
var newCompleteString = this.responseText;
I am trying to figure out the best way to parse the string into a key: value array
</div>
What you want seems to be basically the same as parsing the query string.
See this question How can I get query string values in JavaScript?
const getParams = query => {
if (!query) {
return { };
}
return (/^[?#]/.test(query) ? query.slice(1) : query)
.split('&')
.reduce((params, param) => {
let [ key, value ] = param.split('=');
params[key] = value ? decodeURIComponent(value.replace(/\+/g, ' ')) : '';
return params;
}, { });
};
console.log(
getParams('firstName=first&lastName=last&address=addr&province_id=322&city_id=11')
);
</div>