I have 2 arrays. One for that contains keys and the other that contains input values, like so:
var keys = ['id', 'name', 'category'];
var values = ['1', 'nameVal', 'catVal'];
Now what I'm trying to do is create a data string using a loop to use for a jQuery Ajax function:
var dataString = 'id=1&name=nameVal&category=catVal';
$.ajax({
data: dataString,
...
});
So I tried writing a function like so:
function generateDataString(keys, values, ID)
{
var dataString = 'id=' + ID;
for (var i = 0; i < values.length; ++i)
{
var dataString += '&' + SOMETHINGHERE + '=' + values[i];
}
return dataString;
}
I have already pushed values in the for loop, but where it says SOMETHINGHERE, I need to get the keys too.
I've also used the += operator which I assume is the correct way to go about this, however I'm getting the following error:

Does anyone know where I'm going wrong and how I can also use the key in the dataString?
If it's easier to use jQuery to achieve this, then that's perfectly fine too
dataString += '&' + SOMETHINGHERE + '=' + values[i];withoutvarvaryou are defining the local variable, not changing the parent function variable.