1

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:

enter image description here

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

2
  • Try using dataString += '&' + SOMETHINGHERE + '=' + values[i]; without var Commented Jul 15, 2015 at 14:03
  • when using var you are defining the local variable, not changing the parent function variable. Commented Jul 15, 2015 at 14:04

4 Answers 4

1

You must get rid of the var inside the for loop.

var keys   = ['id', 'name', 'category'];
var values = ['1', 'nameVal', 'catVal'];

 var dataString = 'id=' + '28';

    for (var i = 0; i < values.length; ++i) 
    {
       dataString += '&' + keys[i] + '=' + values[i];
    }

alert(dataString);

Working jsfiddle: https://jsfiddle.net/fLo65noL/

I modify your SOMETHINGHERE with keys[i] and i assume that your ID is 28.

And you will have something like: id=28&id=1&name=nameVal&category=catVal

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

2 Comments

Thank you for this. I thought I had to use another for loop of some sort to get keys[i] but obviously not :)
If you have the same number of values and keys this will work good.
1

Replace:

var dataString += '&' + SOMETHINGHERE + '=' + values[i];

With:

dataString += '&' + SOMETHINGHERE + '=' + values[i];

You can't "add" to a variable you're declaring. (That declaration shouldn't be there in the first place)

1 Comment

@Lodder: Time to take a break ;-)
1

Your bug is the fact you have a var on that line. Remove the var.

BUT There is no need to generate the string, use an object.

var keys   = ['id', 'name', 'category'];
var values = ['1', 'nameVal', 'catVal'];

var data = {};
for (var i=0;i<keys.length;i++){
    data[keys[i]] = values[i];
}

$.ajax({
    data: data,
    ...
});

In reality there is no need to have the two arrays, just have the object to start.

var data = {
   'id' : "1",
   'name' : "nameVal",
   'category': "catVal"
};

1 Comment

The 2 arrays are generated via the dynamics of the way the page work (too long to go into). But thanks for the comment about the object.
0

This part of your code cause the problem

 for (var i = 0; i < values.length; ++i) 
    {
        var dataString += '&' + SOMETHINGHERE + '=' + values[i]; // This one
    }

What you are trying to do here is that you want to declare variable "dataString" but when you it's created, append some string to variable content.

You have delared variable before the loop, so there is no need to redeclare it again with each step in the loop, especially if you want to append some string with each step.

 for (var i = 0; i < values.length; ++i) 
    {
        dataString += '&' + SOMETHINGHERE + '=' + values[i]; // This one
    }

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.