0

I am have a set of boolean value as following

var prodcat1 = true;
var prodcat2 = false;
var prodcat3 = false;
var prodcat4 = false;
var prodcat5 = false;
var prodcat6 = false;
var prodcat7 = false;
var prodcat8 = false;
var prodcat9 = false;
var prodcat10 = true;

How can I convert all the true variable into one array as following.

  var array = ["prodcat1", "prodcat10"]
1
  • 4
    Why isn't this an array like [true, false, ..., true] to begin with? Commented Dec 28, 2018 at 8:53

5 Answers 5

2

You somewhat painted yourself in a corner by using variable names like prodcat5, because you can't really iterate through them, without using the dreaded eval() function.

let arr = [];

for (let i=1; i<=10; i++) {
  if (eval("prodcat" + i)) {
    arr.push("prodcat" + i);
  }
}

This is quite a bad way of doing it, eval() can, in general, present a security risk.

Another solution is, perhaps better (if you're running the code in a browser), is what ThatBrianDude came up with (look below), by (ab)using the window object.

But all these solutions are flawed, because the problem can be easily avoided. A much better solution is to have an array called prodcat and storing values in it like this:

prodcat[0] = true;
prodcat[1] = false;
/* etc... */

Then you can easily iterate through them.

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

Comments

1

What you are doing here is very wrong. Its possible yes, but you arent leveraging what arrays are made for.

To anwser your question anyway, you could to it like this:

let trueArray = [];

for(let i = 1; i <=10; i++){
    if(window["prodcat" + i])
    trueArray.push("prodcat" + i)
}

console.log(trueArray)

1 Comment

"prodcat" + 1 should be "prodcat" + i, but other than that, this is a good solution to a problem that could have been easily avoided. :)
0

Your way of using variable and then storing variable name in array is not optimal. In this way you manually need to push variable names if true.

var prodcat1 = true;
var prodcat2 = false;
var prodcat3 = false;
..  ...        ...
..  ...        ...
var prodcat10 = true;

var myArr = []
//now test for values and push in myArr manually

if (prodcat1) {
    myArr.push('prodcat1')
}
if (prodcat2) {
    myArr.push('prodcat2')
}

The other approach is using eval. But eval is very unpredictable and should be avoided.

for (let i = 1; i <= 10; i++) {
    if (eval('prodcat' + i)) {
        myArr.push('prodcat' + i)
    }
}

The best approach as per me would be using an object to store your values.

var myProdcats = {
    prodcat1: true,
    prodcat2: false
}
var myArray = Object.keys(myProdcats).filter(prodcat => myProdcats[prodcat])  // ["prodcat1", "prodcat2"]

Comments

0

You can do:

const prodcat1 = true;
const prodcat2 = false;
const prodcat3 = false;
const prodcat4 = false;
const prodcat5 = false;
const prodcat6 = false;
const prodcat7 = false;
const prodcat8 = false;
const prodcat9 = false;
const prodcat10 = true;

const result = Array
  .from({length: 10}, (v, i) => i + 1)
  .reduce((a, c) => eval(`prodcat${c}`) ? [...a, `prodcat${c}`] : a, []);

console.log(result);

Comments

0
let arr = [], arr2=[];

for (let i=1; i<=10; i++) {
  if (eval("prodcat" + i)) {
    arr.push(eval("prodcat" + i));
  }else arr2.push(eval("prodcat" + i));
}

would be your solution for now

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.