Chan Yoong Hon
Chan Yoong Hon

Reputation: 1812

Convert a set of "true" boolean value variable into array

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"]

Upvotes: 0

Views: 72

Answers (5)

manish kumar
manish kumar

Reputation: 4692

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

Upvotes: 0

Kresimir
Kresimir

Reputation: 797

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.

Upvotes: 2

Yosvel Quintero
Yosvel Quintero

Reputation: 19070

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);

Upvotes: 0

Ashish
Ashish

Reputation: 4330

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"]

Upvotes: 0

ThatBrianDude
ThatBrianDude

Reputation: 3190

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)

Upvotes: 1

Related Questions