0

I am hoping I am missing something obvious here, but I have tried for the past half day to set two variables to combine in a certain format in Javascript I can do it as a string, however I need it in a different format.

If I select on option from the check boxes {"GILLS":"7"} works fine however if two options are selected query1 should look like [{"GILLS":"1"},{"GILLS":"7"}]. I cannot use += to the variable as this kicks out an unexpected token error.

var Query1 = '';
var Query3 = '';

if ($('input[name=checkbox2a]:checked').length > 0) {
  var  Query3 = {"GILLS":"7"};
}

if ($('input[name=checkbox2b]:checked').length > 0) {
  var Query3 = {"GILLS":"1"};
}

4 Answers 4

1

Try

  var Query1 = [];

and in your function use

Query1.push({"GILLS":"1"})

So the change will be like below

var Query1 = [];
var Query3 = [];

if ($('input[name=checkbox2a]:checked').length > 0) {
    Query3.push({"GILLS":"7"});
}
if ($('input[name=checkbox2b]:checked').length > 0) {
    Query3.push({"GILLS":"1"});                    
}

then you can use join will give you string

Query3.join(", ")
Sign up to request clarification or add additional context in comments.

Comments

0

Make an array out of Query3, if you still want a string at the end, use .join("").

var Query1 = '';
var Query3 = [];

if ($('input[name=checkbox2a]:checked').length > 0) {
  Query3.push({"GILLS":"7"});
}

if ($('input[name=checkbox2b]:checked').length > 0) {
  Query3.push({"GILLS":"1"});                    
}

Query3 = Query3.join(""); // back to string, if you so like

Comments

0

You're causing pain for yourself if you take the approach that sometimes the variable is of type Object ({"GILLS": "1" }) and sometimes it is of type List ([{"GILLS":"1"},{"GILLS":"7"}]).

Since it sometimes needs to be a list, make sure it's always a list. Even though sometimes it's a list of length 1. Then when you are reading that value, you never need conditional logic to deal with two potential types.

So, it's always a List:

var query3 = [];

... and then it's easy to manipulate:

if(...) {
    query3.push( { "GILLS": "7" } );
}
if(...) {
    query3.push( { "GILLS": "1" } );
}

Comments

0

At first, you are declaring same variable in a different scope. Don't do this unless uou know what you are doing. In other words use var keyword only once for a variable in a "space" when you are using this particular variable.

An advantage/disadvantage (chose one ;)) of javascript is that there is no type control. If You type var Query3 = ''; and then Query3 = {"GILLS":"7"}; interpreter will execute this without any complaints beside that there is high probability, that this is not exactly what you want to do ;). In this case it makes variable Query3 an empty string and then makes it an object.

In Your case, You want to have in result an array of objects. At first declare var Query3=[]; and then use a push method on it (Query3.push({});) to add elements.

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.