0

How can I loop into two arrays and create string output from both arrays statArr and typeArr with appending some AND & OR operators?

What I am hoping to get at the output is

stat = 'Closed' AND type = 'RD' OR
stat = 'Closed' AND type = 'WW' OR
stat = 'Closed' AND type = 'CC' OR
stat = 'Closed' AND type = 'MB' OR
stat = 'Closed' AND type = 'CN' OR
stat = 'Investigation' AND type = 'RD' OR
stat = 'Investigation' AND type = 'WW' OR
stat = 'Investigation' AND type = 'CC' OR
stat = 'Investigation' AND type = 'MB' OR
stat = 'Investigation' AND type = 'CN' 

var statArr = ["Closed", "Investigation"];
var typeArr = ["RD", "WW", "CC", "MB", "CN" ];
var sql = "";
for (let i = 0; i < statArr.length; i++) {
  sql += "stat = " + statArr[i] + " AND Type =";
 }
 console.log(sql);

2
  • What should be the result? Commented Sep 4, 2022 at 18:39
  • this stat = 'Closed' AND type = 'RD' OR stat = 'Closed' AND type = 'WW' OR stat = 'Closed' AND type = 'CC' OR stat = 'Closed' AND type = 'MB' OR stat = 'Closed' AND type = 'CN' OR stat = 'Investigation' AND type = 'RD' OR stat = 'Investigation' AND type = 'WW' OR stat = 'Investigation' AND type = 'CC' OR stat = 'Investigation' AND type = 'MB' OR stat = 'Investigation' AND type = 'CN' Commented Sep 4, 2022 at 18:40

2 Answers 2

2

Nest two for loops to generate each stat/type combination, then join them with ORs. I added parentheses to each clause just to make sure precedence works as it should.

var statArr = ["Closed", "Investigation"];
var typeArr = ["RD", "WW", "CC", "MB", "CN" ];
var clauses = [];
for(var stat of statArr) {
  for(var type of typeArr) {
    clauses.push(`(stat = '${stat}' AND type = '${type}')`);
  }
}
var sql = clauses.join(" OR ");

The output is

(stat = 'Closed' AND type = 'RD') OR (stat = 'Closed' AND type = 'WW') OR (stat = 'Closed' AND type = 'CC') OR (stat = 'Closed' AND type = 'MB') OR (stat = 'Closed' AND type = 'CN') OR (stat = 'Investigation' AND type = 'RD') OR (stat = 'Investigation' AND type = 'WW') OR (stat = 'Investigation' AND type = 'CC') OR (stat = 'Investigation' AND type = 'MB') OR (stat = 'Investigation' AND type = 'CN')

However, I think you might really just want

stat IN ('Closed', 'Investigation') AND type IN ('RD', 'WW', 'CC', 'MB', 'CN')

which could be generated with

const quote = s => `'${s}'`;
var sql = `stat IN (${statArr.map(quote).join(", ")}) AND type IN (${typeArr.map(quote).join(", ")})`;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks AKX, can you please also let me know how to achieve the second solution?
0

Using nested for loop, you can get result.

    var statArr = ["Closed", "Investigation"];
    var typeArr = ["RD", "WW", "CC", "MB", "CN" ];
    var sql = "";

    for (let i=0; i<statArr.length; i++) {
        for(let j=0; j<typeArr.length; j++){
            console.log(`stat = '${statArr[i]}' AND type = '${typeArr[j]}' OR`)
        }
    }

1 Comment

This will include a final OR, which is not desired.

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.